Search code examples
reactjsreduxredux-toolkit

Can not fetch data using react redux-toolkit


I could not find any problem in my code , seems all ok. But data fetching is not working. please help me. here is the code :

the store :

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

the view::


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;

Solution

  • Well, Got the solution ,

    I should call the fetchUshers() function back into the dispatch function :

    const dispatch =useDispatch()
    
        useEffect(() => {
            dispatch(fetchUsers())
    
            console.log('profiles',profiles);
            console.log('error',error);
        }, [])