【发布时间】:2020-02-21 08:20:14
【问题描述】:
我正在尝试在我的应用程序中创建切换开关,我是 redux 的新手。在非常努力并在网上到处寻找之后发布这个问题。请帮助。 我的减速机如下:
const initialState = {
preview: "off",
cards:
};
const cardReducer = (state = initialState, action) => {
switch (action.type) {
case "FIND_ALL_CARDS":
return {
cards: action.cards
};
case "DELETE_CARD":
return {
cards: state.cards.filter(card => card.id !== action.cardId)
};
case "CREATE_CARD":
return {
cards: [...state.cards, action.card]
};
case "PREVIEW":
console.log(state); // I get cards[] in the state but no preview.
let swtch = "on";
if (state.preview === "on") {
swtch = "off";
}
console.log(state.preview); // Undefined
return {
cards: state.cards,
preview: swtch
};
default:
return state;
}
我的组件已正确连接
const stateToPropertyMapper = state => ({
cards: state.cards.cards,
preview: state.cards.preview
});
【问题讨论】:
-
let swtch = "on"为什么不isSwitchOn = true?更好的是,你为什么还有swtch?只需返回preview: !state.preview。您的状态值是一个布尔值,那么为什么要将它与字符串进行比较? -
我已编辑。我也尝试过这样做。第一次状态:预览未定义,但再次单击切换开关后,我的控制台中出现“开启”、“关闭”
-
添加了我的控制台的图片。任何见解请@JMadelaine
标签: javascript reactjs redux react-redux web-deployment-project