- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个基本的待办事项列表。待办事项列表的待办事项输入字段有一个 onChange 事件,该事件触发一个操作并将 event.target.value 发送到 reducer,并将用户键入的每个字符存储到存储对象的属性中。
当用户提交表单时,我想获取之前通过 onChange 事件存储的数据,然后我想将它放在存储对象的新属性上。
我如何获取之前从商店输入的数据并将其拉入不同的 reducer ?
在我见过的所有示例中,reducer 都以“初始状态”开始。我不想要那个,我想要用户输入的先前状态。
下面是代码的 CodeSandbox 版本(出于某种原因,右侧的橙色-米色选项卡需要切换到左侧以呈现表单。如果您不这样做,它就不会工作)。
最佳答案
Ask yourself whether you've structured your reducers correctly. If a and b are not independent of one another, why are they separate reducers?
好吧,如果我们谈论正确构造 reducer,cachedInput
和 todoList
应该存在于单个 reducer 中。无需创建另一个 reducer 。另外我猜你需要一次获取所有输入值(当用户点击提交时)并将其发送到存储。
如果你仍然想创建单独的 reducer,那么你可以按照下面给出的方法:
回答如何访问或共享已经存储在 reducer 中的数据?
不要使用 combineReducers。
示例
替换这段代码
export const a = combineReducers({
cachInput,
newTodo
});
与
export default (state = {}, action) => {
return {
cachInput: cachInput(state.app, action, state),
newTodo: newTodo(state.posts, action, state),
};
};
reducer 会像
const reducer = (state = initialState, action, root) => {....}
并且您可以从root
关于javascript - react 终极版 : How to share data between reducers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50791074/
我是一名优秀的程序员,十分优秀!