Answer the question
In order to leave comments, you need to log in
Why is redraw not happening in self-written react-redux?
Hello everyone, this is how I change my state:
store itself:
const initialState = { firstname: "" };
const reducer = (state = initialState, action) => {
switch (action.type) {
case "SET_FIRSTNAME":
state.firstname = action.payload.firstname;
return {
...state,
};
default:
return state;
}
};
export const createStore = (reducer) => {
let state = reducer(undefined, {});
let callback;
return {
dispatch: (action) => {
reducer(state, action);
callback();
},
getState: () => state,
subscribe: (cb) => {
callback = cb;
},
};
};
export const store = createStore(reducer);
store.subscribe(() => {
setUser(store.getState());
});
store.subscribe(() => {
setUser({ ... store.getState()});
});
setUser
- f-i for change useState
(due to what the change occurs)
Answer the question
In order to leave comments, you need to log in
state.firstname = action.payload.firstname;
here you are mutating the previous state, potentially making it impossible to compare states.
reducer(state, action);
the returned state is not written anywhere.
callback();
But here there can be a potential error, since the callback can be undefined
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question