我正在使用 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.ts
export const setCategory = (name) => ({
type: 'SET_CATEGORY',
name: name
});
存储/reducers/index.ts
import {combineReducers} from 'redux';
import category from './category';
const app = combineReducers({
category
});
export default app;
存储/reducers/category.ts
const 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;
我敢肯定,我缺少一些小细节。
胡说叔叔
相关分类