mardi 3 novembre 2020

Updating Redux State from response data

I have a login/signup system for my React App.

I want to set the user state to the response recieved after fetch. The code looks like this -

export const userSlice = createSlice({
  name: "user",
  initialState: {
    user: null,
  },
  reducers: {
    login: (state, action) => {
      fetch("http://localhost:5000/user/login", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(action.payload),
      })
        .then((res) => {
          return res.json();
        })
        .then((data) => {
          console.log(data);
          state.user = data;
        });
    },
  },
});

Everything works fine until I add the line state.user = data , after which I get an error which says -

Unhandled Rejection (TypeError): Cannot perform 'set' on a proxy that has been revoked

What am I doing wrong?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire