简体   繁体   English

React Hook 设置 state 在 useEffect 与 state 在依赖数组

[英]React Hook setting state in useEffect with state in dependency array

I have a question about the correct usage with useEffect and setState in React Hooks.我有一个关于在 React Hooks 中正确使用 useEffect 和 setState 的问题。

here is my react app that gets a persisted configuration and some other data depending on the config like this:这是我的反应应用程序,它根据如下配置获取持久配置和一些其他数据:

function App() {
    const [config, setConfig] = useState(null);
    // some other states 

    useEffect(() => {
        const fetchData = () => {
            axios.get("/path/to/config").then(response => {
                if (response.status === 200) {
                    return response.data
                }
            })
                .then(data => {
                    setConfig(data.config);
                    // set some other states
                })
                .catch(error => {
                    console.log("Error:" + error);
                })
        }
        fetchData();
    }, [config]);

    return (
        <div >
            ...
        </div>
    );
}

If I run this App useEffect is instantly called twice one time for first render and then a second time because setConfig(data.config);如果我运行这个应用程序 useEffect 立即调用两次,第一次渲染,然后第二次,因为setConfig(data.config); is called in the axios get success function.在 axios 中调用得到成功 function。

The user has the possibility to change his own config that is done in another request.用户可以更改在另一个请求中完成的自己的配置。 If he does I want after state config changes that the config and some other data depending on the config is reloaded via this useEffect function.如果他确实想要在 state 配置更改后通过此 useEffect function 重新加载配置和一些其他取决于配置的数据。

Now since there is no setstate callback in react hooks I read somewhere I should use useEffect with the state variable in the dependency array.现在,由于反应钩子中没有 setstate 回调,我在某处读到我应该使用 useEffect 和依赖数组中的 state 变量。

How can I prevent that my useEffect is called twice right at the beginning?如何防止我的 useEffect 在开始时被调用两次?

I have the feeling that I am doing it all wrong.我有一种感觉,我做错了。 Thank you in advance先感谢您

You need to add an if condition in useEffect, because useEffect is called by default on first render.您需要在 useEffect 中添加一个 if 条件,因为在第一次渲染时默认调用 useEffect。

useEffect(() => {
      if(config !== null){
        const fetchData = () => {
            axios.get("/path/to/config").then(response => {
                if (response.status === 200) {
                    return response.data
                }
            })
                .then(data => {
                    setConfig(data.config);
                    // set some other states
                })
                .catch(error => {
                    console.log("Error:" + error);
                })
        }
        fetchData();
      }
    }, [config]);

This is not right.这个不对。 I guess this can call fetchData infinitely.我想这可以无限调用 fetchData。

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

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