5 examples of 'useeffect previous state' in JavaScript

Every line of 'useeffect previous state' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
3export default function usePrevious(value) {
4 const ref = useRef();
5 useEffect(() => {
6 ref.current = value;
7 });
8 return [ref.current];
9}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

32_applyEffect() {
33 super._applyEffect();
34 const player = this.game.player;
35 this.effectTypes.forEach((effectType) => {
36 const amount = this._getAmountForEffect(effectType);
37 if(amount !== null) {
38 const event = Mediator.publish(EffectItemMessages.APPLY, {
39 source: this.entity,
40 effectType: effectType,
41 amount: amount
42 });
43 if(event.process) {
44 switch (event.args.effectType) {
45 case ItemEffectType.HEALTH:
46 player.increaseHealth(event.args.amount);
47 break;
48 case ItemEffectType.POINTS:
49 player.increasePoints(event.args.amount);
50 break;
51 case ItemEffectType.MONEY:
52 player.increaseMoney(event.args.amount);
53 break;
54 }
55 }
56 }
57 });
58}
82function effectCurrent( func ) {
83 chrome.tabs.query( {}, function( s ) {
84 let activeIndex = -1;
85
86 for( let i = 0; i < s.length; i++ ) {
87 if ( s[i].active ) {
88 activeIndex = i;
89 break;
90 }
91 }
92 if ( activeIndex >= 0 ) {
93 // tab, tab index, tab id
94 func( s[activeIndex], activeIndex, s[activeIndex].id );
95 }
96 } );
97}
51function Effect() {
52 const { gl, scene, camera, size } = useThree()
53
54 const [bloom, final] = useMemo(() => {
55 const renderScene = new RenderPass(scene, camera)
56
57 const comp = new EffectComposer(gl)
58 comp.renderToScreen = false
59 comp.addPass(renderScene)
60 comp.addPass(new UnrealBloomPass(new THREE.Vector2(size.width, size.height), 1.25, 1, 0))
61
62 const finalComposer = new EffectComposer(gl)
63 finalComposer.addPass(renderScene)
64 const finalPass = new ShaderPass(
65 new THREE.ShaderMaterial({
66 uniforms: { baseTexture: { value: null }, bloomTexture: { value: comp.renderTarget2.texture } },
67 vertexShader:
68 'varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); }',
69 fragmentShader:
70 'uniform sampler2D baseTexture; uniform sampler2D bloomTexture; varying vec2 vUv; vec4 getTexture( sampler2D texelToLinearTexture ) { return mapTexelToLinear( texture2D( texelToLinearTexture , vUv ) ); } void main() { gl_FragColor = ( getTexture( baseTexture ) + vec4( 1.0 ) * getTexture( bloomTexture ) ); }',
71 }),
72 'baseTexture'
73 )
74 finalPass.needsSwap = true
75 finalComposer.addPass(finalPass)
76
77 const fxaa = new ShaderPass(FXAAShader)
78 fxaa.material.uniforms['resolution'].value.x = 1 / size.width
79 fxaa.material.uniforms['resolution'].value.y = 1 / size.height
80 finalComposer.addPass(fxaa)
81
82 return [comp, finalComposer]
83 }, [])
84
85 useEffect(() => {
86 bloom.setSize(size.width, size.height)
87 final.setSize(size.width, size.height)
88 }, [bloom, final, size])
89
90 useFrame(() => {
91 // https://github.com/mrdoob/three.js/blob/master/examples/webgl_postprocessing_unreal_bloom_selective.html
92 // this seems kinda dirty, it mutates the scene and overwrites materials
93 scene.traverse(darkenNonBloomed)
94 bloom.render()
95 scene.traverse(restoreMaterial)
96 // then writes the normal scene on top
97 final.render()
98 }, 1)
99 return null
100}
82effect(value) {
83 this.lightState.effect(value);
84 return this;
85}

Related snippets