Reducer loggedReducer:
export default function loggedReducer(state=[], action) {
switch(action.type) {
case 'SIGN_IN':
return state.push(action.payload);
case 'LOG_OUT':
return state = [];
default:
return state;
}
}
Получаю объект пользователя:
const dispatch = useDispatch();
React.useEffect(() => {
async function fetch() {
const {data} = await axios.get('/profile/api/user');
dispatch(signIn(data));
setUser(data);
}
fetch();
}, [dispatch]);
action:
export const signIn = (user) => {
return {
type: 'SIGN_IN',
payload: user
}
}
reducer:
import loggedReducer from './isLogged.js';
import {combineReducers} from 'redux';
const allReducers = combineReducers({
logged: loggedReducer
})
export default allReducers;
Потом в другом компоненте вывожу в консоль logged:
const logged = useSelector(state => state.logged);
const dispatch = useDispatch();
console.log('logged: ' + logged); // 1
Он равен 1, почему так?