我是redux的新手,正在尝试采取行动以在新的reactboilerplate项目(使用immer)中产生简单的状态更新和组件重新渲染。 console.log
确认,动作组件正常工作,并且我没有传递任何数据,只是传递了一个boolean
的动作。
homeVisible
的状态应从false
变为true
。
减速器:
import produce from 'immer';
import { SET_HOME_VISIBLE } from './constants';
// The initial state of the App
export const initialState = {
homeVisible: false
};
/* eslint-disable default-case, no-param-reassign */
const homeReducer = (state = initialState, action) =>
produce(state, draft => {
switch (action.type) {
case SET_HOME_VISIBLE:
draft.homeVisible = true;
break;
}
});
export default homeReducer;
选择器:
/**
* Homepage selectors
*/
import { createSelector } from 'reselect';
import { initialState } from './reducer';
const selectHome = state => state.home || initialState;
const setHomeVisible = state => state.homeVisible || initialState;
const makeHomeVisible = () =>
createSelector(
setHomeVisible,
homeVisibleState => homeVisibleState.homeVisible,
);
export { selectHome, makeHomeVisible, setHomeVisible };
动作(为了连续):
import { SET_HOME_VISIBLE } from './constants';
/**
* Changes the input field of the form
*
* @param {string} username The new text of the input field
*
* @return {object} An action object with a type of CHANGE_USERNAME
*/
export function setHomeVisible(homeVisible) {
return {
type: SET_HOME_VISIBLE,
homeVisible
};
}