无法使用react redux-toolkit提取数据

mftmpeh8  于 2022-11-12  发布在  React
关注(0)|答案(1)|浏览(205)

我找不到任何问题,在我的代码,似乎都可以。但数据提取是不工作。请帮助我。这里是代码:
商店:

export const store = configureStore({
  reducer: {
    counter: counterReducer,
    profiles:ProfileReducer,
  },
});

视图:

const AllUsers = () => {
    const { isLoading, error, profiles } = useSelector(state =>state?.profiles)

const dispatch =useDispatch()

    useEffect(() => {
        dispatch(fetchUsers)

        console.log('profiles',profiles);
        console.log('error',error);
    }, [])

    if(isLoading){
        return <Loading></Loading>
    }

    return (
        <div>
            {profiles && <p>all users{profiles.length}</p>}
        </div>
    );
};

export default AllUsers;
5t7ly7z5

5t7ly7z51#

嗯,找到解决办法了,
我应该将fetchUsers()函数调用回调度函数:

const dispatch =useDispatch()

    useEffect(() => {
        dispatch(fetchUsers())

        console.log('profiles',profiles);
        console.log('error',error);
    }, [])

相关问题