Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Useffect无限循环_Reactjs_Loops_React Hooks - Fatal编程技术网

Reactjs Useffect无限循环

Reactjs Useffect无限循环,reactjs,loops,react-hooks,Reactjs,Loops,React Hooks,尽管传递了一个空数组,但当我尝试更新全局状态时,useEffect仍然会生成一个无限循环。我尝试过不传递空数组,添加依赖项,甚至尝试在组件内部使用useState钩子,但似乎没有任何效果。API调用没有问题,当我删除函数以更新状态时,没有循环。当我在我的代码中的其他地方这样做时,一切都很好。下面是调用useEffect的功能组件 const Posts = () => { const [{auth}] = useAuth(); const [{profile},, setP

尽管传递了一个空数组,但当我尝试更新全局状态时,useEffect仍然会生成一个无限循环。我尝试过不传递空数组,添加依赖项,甚至尝试在组件内部使用useState钩子,但似乎没有任何效果。API调用没有问题,当我删除函数以更新状态时,没有循环。当我在我的代码中的其他地方这样做时,一切都很好。下面是调用useEffect的功能组件

const Posts = () => {
    const [{auth}] = useAuth();
    const [{profile},, setPosts] = useProfile();
    const {getPostsByUser} = PostApi()
    useEffect(() => {
        const fetch = async () => {
            const response = await getPostsByUser(auth.user._id, auth.token)
            setPosts(response)
        }
        fetch()
    }, [])
    console.log(profile)
    return(
        <div className="User-Post">
            <div className="New-Post">
                <NewPost />
            </div>
            <div className="User-Posts-Content">
                {
                    profile.posts ? profile.posts.map((item, key) => {
                        return <Post post={item} />
                    }) : null
                }
            </div>
        </div>
    )
}
这里是全球商店:

function makeStore() {
    // Make a context for the store
    const Context = React.createContext();

    // Make a provider that takes an initialValue
    const Provider = ({ initialValue, children }) => {
      // Make a new state instance (could even use immer here!)
      const [state, setState] = useState(initialValue);
      // Memoize the context value to update when the state does
      const contextValue = useMemo(() => [state, setState], [state]);

      // Provide the store to children
      return <Context.Provider value={contextValue}>{children}</Context.Provider>;
    };

    // A hook to help consume the store
    const useStore = () => useContext(Context);

    return { Provider, useStore };
}
函数makeStore(){
//为商店创建上下文
const Context=React.createContext();
//创建一个接受initialValue的提供程序
常量提供程序=({initialValue,children})=>{
//创建一个新的state实例(甚至可以在这里使用immer!)
常量[状态,设置状态]=使用状态(初始值);
//将要在状态发生时更新的上下文值记录下来
const contextValue=usemo(()=>[state,setState],[state]);
//为儿童提供商店
返回{children};
};
//帮助消费商店的钩子
constUseStore=()=>useContext(上下文);
返回{Provider,useStore};
}
这可能会有所帮助

  const [toggle, setToggle] = useState(false);
   useEffect(() => {
        const fetch = async () => {
            const response = await getPostsByUser(auth.user._id, auth.token)
            setPosts(response)
        }
        fetch()
    }, toggle)

需要时使用此结构

控制台的输出是什么?还有,是什么让你意识到这是一个无限循环?控制台上的输出是api被反复调用,状态被反复更新。这个答案是正确的。这正是我在发布这个问题后所做的,为了我想要实现的目标,使用一个地方政府要比使用全球政府好得多
  const [toggle, setToggle] = useState(false);
   useEffect(() => {
        const fetch = async () => {
            const response = await getPostsByUser(auth.user._id, auth.token)
            setPosts(response)
        }
        fetch()
    }, toggle)