编辑:解决方案将不同的变量连接到mapStateToProps。Redux - 相同的减速器,容器和组件产生不同的结果
我在使用react-redux应用程序时遇到了一些麻烦,我不确定我做错了什么。
源代码可以是found here。
Alphabetical
动作和减速器似乎工作正常,但与Duplicate
逻辑不同,词汇表不会在字母按钮切换时重新显示。我猜这是因为我没有正确地将它挂在商店/派送上。
我创建了Duplicate
reducer,action,容器,组件,一旦我得到那些工作,我将代码复制到Alphabetical
文件。除了变量名,代码应该是相同的,但是当我在createStore中分别运行每个reducer时,duplicates
正常工作,而alphabetical
则不正确。
你可以看到Alphabetical
按钮应该如何在createStore函数中使用toggleDuplicates
(我还在弄清楚为什么组合reducers不起作用 - 也许这与问题有关?)。
的src/entry.jsx
变化allReducers到toggleDuplicates
或toggleAlphabetical
let store = createStore(
allReducers,
initialState,
compose(
applyMiddleware(createLogger())
)
);
要运行,cd进入该目录,然后运行npm install
,npm run server
预期的行为是Duplicate
和Alphabetize
按钮将在切换时使用正确的值更新GlossaryTable。当visibleWords
状态更新时,GlossaryTable
应重新呈现(由toggleDuplicates/toggleAlphabetical减速返回后。
的src /集装箱/ GlossaryContainer.js
这就是我想我可能不正确地布线高达
const mapStateToProps = (state) => ({
visibleWords: getVisibleEntries(
state.words,
state.visibleWords,
state.toggleDuplicates,
state.toggleAlphabetical
)
});
const VisibleGlossary = connect(
mapStateToProps
)(GlossaryTable);
export default VisibleGlossary;
当您创建商店时,请避免initialState,只需将它传递给您的减速器即可。 – Hosar