I have crated the following userSlice
import { createSlice } from "@reduxjs/toolkit";
const initialState = {username: ""};
export const userSlice = createSlice({
name: "user",
initialState,
reducers: {
setUsername(state, action) {
state.username = action.payload;
},
},
});
export const { setUsername } = userSlice.actions;
export default userSlice.reducer;
Now, I wanted to use the dispatch the reducer setUsername
like this
const Login = (props) => {
const classes = useStyles();
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const user = useSelector((state) => state.user.username);
const dispatch = useDispatch();
useEffect(() => {
dispatch(getUsers());
}, [dispatch]);
const users = useSelector((state) => state.users.users);
const validateUser = () => {
const usernames = Object.keys(users);
if (usernames.includes(username)) {
if (users[username].password === password) {
console.log(username);
dispatch(
setUsername({
payload: username,
})
);
}
}
};
But this gives me the error Actions must be plain objects. Use custom middleware for async actions.
. After trying to resolve this issue for one hour I must admit that I have no clue how to resolve this problem. What am I doing wrong?
//store.js
import { configureStore } from "@reduxjs/toolkit";
import userReducer from "../features/user/userSlice";
import usersReducer from "../features/users/usersSlice";
export const store = configureStore({
reducer: {
user: userReducer,
users: usersReducer,
},
});