//here is my action.js
export const update = user => ({
type: 'UPDATE_USER',
payload: user,
});
// Here is my reducer.js
const initialState = {
name: '',
logo:'',
mobile:'',
};
export default (state = initialState, action) => {
const { type, payload } = action;
switch (type) {
case 'UPDATE_USER':
return {
...state,
user: payload.user,
};
default:
return state;
}
};
//Here is my store.js
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import userReducer from "../reducers/userReducer";
const middleware = [thunk];
const store = createStore(userReducer, applyMiddleware(...middleware));
export default store;
//Here i am setting data
let user = {
name: 'abcd',
logo: result.payload.data.logo,
mobile: '0123456789'
};
dispatch(update(user));
// Trying to get the value in component
const state = useSelector((state) => state);
console.log('statevalue',state);
I am using redux to store loggedIn user data to render with UI Component.After setting data the values in console printing empty.The user details is not updated please help me to fix this.
Note value in console 'statevalue', { name: '', logo: '', mobile: '', user: undefined }
Expected Output 'statevalue', { name: 'abcd', logo: 'https://lfkgjkfk', mobile: '0123456789'}
CodePudding user response:
Update your reducer with this change -
case 'UPDATE_USER':
return {
...state,
...payload,
};