我正在使用 React-Redux 的 React Hooks 实现。下面是我的代码流程。由于某种原因,我传入的任何值dispatch(fuction(value))都没有在我的减速器中检测到。我想不通。src/components/categories/selectCategory.tsx import React from 'react'; import {useDispatch} from 'react-redux'; import {setCategory} from '../../store/actions'; const selectCategory = (name: string) => { dispatch(setCategory(name)); // ex: name = 'algebra' };存储/操作/index.tsexport const setCategory = (name) => ({ type: 'SET_CATEGORY', name: name});存储/reducers/index.tsimport {combineReducers} from 'redux';import category from './category';const app = combineReducers({ category});export default app;存储/reducers/category.tsconst initialState = { name: 'calculus'};const category = (state = initialState, action) => { switch (action.type) { case 'SET_CATEGORY': return {name: state.name}; // outputs 'calculus' default: return state; }};export default category;我敢肯定,我缺少一些小细节。
1 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
我的问题是通过返回action.name属性而不是state.name.
存储/reducers/category.ts
const initialState = {
name: 'calculus'
};
const category = (state = initialState, action) => {
switch (action.type) {
case 'SET_CATEGORY':
return {name: action.name};
default:
return state;
}
};
export default category;
添加回答
举报
0/150
提交
取消