简体   繁体   English

如何在 useEffect Hook 中重新渲染组件

[英]How to rerender component in useEffect Hook

Ok so:好的,所以:

  useEffect(() => {

  }, [props.lang]);

What should I do inside useEffect to rerender component every time with props.lang change?每次使用 props.lang 更改时,我应该在 useEffect 中做什么来重新渲染组件?


Solution:解决方案:

I solve this problem in changing some idea of problem (I use another function to work's well with language change.我通过改变一些问题的想法来解决这个问题(我使用另一个 function 来很好地适应语言变化。

Think of your useEffect as a mix of componentDidMount, componentDidUpdate, and componentWillUnmount, as stated in the React documentation.如 React 文档中所述,将您的 useEffect 视为 componentDidMount、componentDidUpdate 和 componentWillUnmount 的混合体。

To behave like componentDidMount, you would need to set your useEffect like this:要表现得像 componentDidMount,你需要像这样设置你的 useEffect:

  useEffect(() => console.log('mounted'), []);

The first argument is a callback that will be fired based on the second argument, which is an array of values.第一个参数是一个回调,它将基于第二个参数触发,第二个参数是一个值数组。 If any of the values in that second argument change, the callback function you defined inside your useEffect will be fired.如果第二个参数中的任何值发生变化,您在 useEffect 中定义的回调 function 将被触发。

In the example I'm showing, however, I'm passing an empty array as my second argument, and that will never be changed, so the callback function will be called once when the component mounts.然而,在我展示的示例中,我将一个空数组作为我的第二个参数传递,并且永远不会更改,因此当组件挂载时将调用一次回调 function。

That kind of summarizes useEffect.那种总结了useEffect。 If instead of an empty value, you have an argument, like in your case:如果你有一个参数而不是一个空值,就像你的情况一样:

 useEffect(() => {

  }, [props.lang]);

That means that every time "props.lang" changes, your callback function will be called.这意味着每次“props.lang”更改时,都会调用您的回调 function。 The useEffect will not rerender your component really, unless you're managing some state inside that callback function that could fire a re-render. useEffect 不会真正重新渲染您的组件,除非您在可能触发重新渲染的回调 function 中管理一些 state。

UPDATE:更新:

If you want to fire a re-render, your render function needs to have a state that you are updating in your useEffect.如果你想重新渲染,你的渲染 function 需要有一个 state,你正在更新你的 useEffect。

For example, in here, the render function starts by showing English as the default language and in my use effect I change that language after 3 seconds, so the render is re-rendered and starts showing "spanish".例如,在这里,渲染 function 首先显示英语作为默认语言,在我的使用效果中,我在 3 秒后更改了该语言,因此渲染被重新渲染并开始显示“西班牙语”。

 function App() { const [lang, setLang] = useState("english"); useEffect(() => { setTimeout(() => { setLang("spanish"); }, 3000); }, []); return ( <div className="App"> <h1>Lang:</h1> <p>{lang}</p> </div> ); }

Full code:完整代码:

编辑启发式-rubin-pmdjk

Simplest way最简单的方法

Add a dummy state you can toggle to always initiate a re-render.添加一个虚拟 state 您可以切换以始终启动重新渲染。

const [rerender, setRerender] = useState(false);

useEffect(()=>{

    ...
    setRerender(!rerender);
}, []);

And this will ensure a re-render, since components always re-render on state change.这将确保重新渲染,因为组件总是在 state 更改时重新渲染。

You can call setRerender(!rerender) anywhere anytime to initiate re-render.您可以随时随地调用setRerender(!rerender)来启动重新渲染。

const [state, set] = useState(0);

useEffect(() => {
  fn();
},[state])

function fn() {
  setTimeout((), {
    set(prev => prev + 1)
  }, 3000)
}

The code above will re-render the fn function once every 3 seconds.上面的代码将每 3 秒重新渲染一次fn function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM