最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I declare a variable outside of useEffect ? React Hooks - Stack Overflow

programmeradmin2浏览0评论

I'm learning React Hooks, and I'm actually using the useEffect method. No problem at all, but I got some warnings about my variable declarations. Here's an example of what I wrote :

import React, { useRef, useEffect } from 'react';

function App(){
  let containerRef = useRef(null);

  let myVariable;

  useEffect(){
    myVariable = containerRef.children[0];
  }

  return(
    <div className="container" ref={el => containerRef = el}>
        <h1>Hey, I'm Laurie </h1>
        <p> Nice to e-meet you!</p>
    </div>
  )
}

This is just an easy and unpleted example of what I did, for animating my website with GSAP. I access to the DOM elements with useRef and I only found this solution. But my console write me some warnings, and I'm pretty lost.

Warning I got :

Assignments to the myVariable variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect.

Can someone help me with this issue ?

I'm learning React Hooks, and I'm actually using the useEffect method. No problem at all, but I got some warnings about my variable declarations. Here's an example of what I wrote :

import React, { useRef, useEffect } from 'react';

function App(){
  let containerRef = useRef(null);

  let myVariable;

  useEffect(){
    myVariable = containerRef.children[0];
  }

  return(
    <div className="container" ref={el => containerRef = el}>
        <h1>Hey, I'm Laurie </h1>
        <p> Nice to e-meet you!</p>
    </div>
  )
}

This is just an easy and unpleted example of what I did, for animating my website with GSAP. I access to the DOM elements with useRef and I only found this solution. But my console write me some warnings, and I'm pretty lost.

Warning I got :

Assignments to the myVariable variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect.

Can someone help me with this issue ?

Share Improve this question edited Mar 4, 2020 at 13:47 SecretAgentMan 2,8547 gold badges24 silver badges43 bronze badges asked Mar 2, 2020 at 16:54 Laurie VinceLaurie Vince 1332 gold badges2 silver badges8 bronze badges 1
  • This snippet is not a valid javascript, also, the warning explains the problem well, what kind of answer are you looking for? How to approach the problem better? – Dennis Vash Commented Mar 2, 2020 at 17:01
Add a ment  | 

1 Answer 1

Reset to default 12

when you are using Hook basically you are telling React that your ponent needs to do something after render. React will remember the function you passed, and call it later after performing the DOM updates.

More Details

https://reactjs/docs/hooks-effect.html#example-using-hooks

Regarding warning: React doesn't remember your myVariable. It will be recreated on each render. It will be better to use useState hook to set value in useEffect and remember it on the next render.

Remember one thing always pass the second argument in useEffect with an array of dependencies.

发布评论

评论列表(0)

  1. 暂无评论