I'm working on a react application where I try to fetch users' data from API. Below is the redux code:
Users Slice Code
`http://${process.env.REACT_APP_API_DOMAIN}/userProfiles`
).then((res) => res.json())
// return response.json();
});
const usersSlice = createSlice({
name: "user",
initialState,
extraReducers: (builder) => {
builder.addCase(fetchUsers.pending, (state) => {
state.isLoading = true;
});
builder.addCase(fetchUsers.fulfilled, (state, action) => {
state.isLoading = false;
state.data = action.payload;
});
builder.addCase(fetchUsers.rejected, (state, action) => {
console.log("Error", action.payload);
state.error = action.error.message;
});
},
});
Users Component Code
const dispatch = useDispatch();
const usersState = useSelector((state) => state.user.data.data);
useEffect(() => {
dispatch(fetchUsers());
setUsersData(usersState)
}, [dispatch]);
At first, when I run the app it works well but when I reload the page it gives the error
Uncaught TypeError: Cannot read properties of null (reading 'data')
This means after reloading it won't get the data, even after reloading the page request is successfully called but it won't get the data.