javascript - React 中的 useEffect 在更新存储后不更新组件

标签 javascript reactjs redux use-effect react-state-management

我不明白为什么 React 不更新我的对象。在另一个组件中,我通过调度更新了状态。在此(在下面的代码中) mapStateToProps 类别中的代码正在更改(控制台日志显示更多类别)。但是组件不会重新渲染,尽管在 useEffect 的组件中我使用了 props.categories。元素中的事件 console.log 不运行

const LeftSidebar = (props: any) => {
    console.log('not render after props.categories changed')
    useEffect(() => {
        props.dispatch(getCategories())
    }, [props.categories]);

    const addCategoryHandler = (categoryId: number) => {
        props.history.push('/category/create/' + categoryId)
    };

    return (
        <div className='left-sidebar'>
            <Logo/>
            <MenuSidebar categories={props.categories} onClickAddCategory={addCategoryHandler}/>
        </div>
    );
};

function mapStateToProps(state: State) {
    const categories = state.category && state.category.list;
    console.log('this categories changes, but LeftSidebar not changing')
    console.log(categories)
    return { categories };
}

export default connect(mapStateToProps)(LeftSidebar);

我想如果我更新状态,根据这个状态更新组件。它应该如何工作?它应该如何工作?可能有用,添加类别的item不是parent也不是child,是neighbor 我的 reducer

import {CATEGORIES_GET, CATEGORY_CREATE} from "../actions/types";

export default function (state={}, action: any) {
    switch (action.type) {
        case CATEGORIES_GET:
            return {...state, list: action.payload};
        case CATEGORY_CREATE:
            return {...state, list: action.payload};
        default: return state;
    }
}

感谢您解决问题。所有问题都在不可变数据中。我使用了固定装置,但没有正确复制数组

import {CATEGORIES_GET, CATEGORY_CREATE} from "./types";
import {categoryMenuItems as items} from "../../fixtureData";
import {NewCategory} from "../../types";

let categoryMenuItems = items; // My mistake, I used not immutable value. Not use fixtures for state))
let id = 33;

export function getCategories() {
    return {
        type: CATEGORIES_GET,
        payload: categoryMenuItems
    }
}

export function createCategory(newCategory: NewCategory) {
    id++
    const category = {
        title: newCategory.name,
        id: id
    };

    // MISTAKE I use same array, not cloned like let clonedCategoryMenuItems = [...categoryMenuItems]
    categoryMenuItems.push(category);

    return {
        type: CATEGORY_CREATE,
        payload: categoryMenuItems
    }
}

不为状态使用固定装置,使用真正的 api :)

最佳答案

也许你的状态不是一成不变的。在你的 reducer 中使用 spread 运算符来添加新的项目

{
    list: [
       ...state.list,
       addedCategory
    ]
}

代替

state.list.push(addedCategory)

关于javascript - React 中的 useEffect 在更新存储后不更新组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59327862/

相关文章:

javascript - 在jquery中创建异步each循环

javascript - 在 React 函数中声明函数

reactjs - redux-saga-test-plan 中 testSaga 和 ExpectSaga 的区别

javascript - React - Redux 根据 UI 状态分派(dispatch)一个 Action

javascript - 多行字符串在 JavaScript 上无法正确读取

javascript - 用 React 猜数字 - 在这种情况下使用状态的正确方法是什么

javascript - 使用 requirejs 的句柄 block 辅助函数(在数组的每一行上添加简单的奇偶属性)

reactjs - 我们如何使用 api 调用加载翻译而不是在静态 jsons 中定义它们?这如何在 React-i18next 中完成?

javascript - 触发 takeUntil 操作时执行函数

带有条件语句的 Redux useSelector