React Warning: Cannot update a component from inside the function body of a different component

前端 未结 10 1152
攒了一身酷
攒了一身酷 2021-01-01 12:24

I am using Redux with Class Components in React. Having the below two states in Redux store.

{ spinner: false, refresh: false }

In Parent C

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 12:31

    If your code calls a function in a parent component upon a condition being met like this:

    const ListOfUsersComponent = ({ handleNoUsersLoaded }) => {
        const { data, loading, error } = useQuery(QUERY);
    
        if (data && data.users.length === 0) {
            return handleNoUsersLoaded();
        }
    
        return (
            

    Users are loaded.

    ); };

    Try wrapping the condition in a useEffect:

    const ListOfUsersComponent = ({ handleNoUsersLoaded }) => {
        const { data, loading, error } = useQuery(QUERY);
    
        useEffect(() => {
            if (data && data.users.length === 0) {
                return handleNoUsersLoaded();
            }
        }, [data, handleNoUsersLoaded]);
    
        return (
            

    Users are loaded.

    ); };

提交回复
热议问题