React Redux 不更新状态

我正在使用 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;

我敢肯定,我缺少一些小细节。



尚方宝剑之说
浏览 226回答 1
1回答

胡说叔叔

我的问题是通过返回action.name属性而不是state.name.存储/reducers/category.tsconst initialState = {    name: 'calculus'};const category = (state = initialState, action) => {    switch (action.type) {        case 'SET_CATEGORY':            return {name: action.name};        default:            return state;    }};export default category;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript