【问题标题】:useEffect in React not update component after update store更新存储后React中的useEffect不更新组件
【发布时间】:2020-04-07 05:15:04
【问题描述】:

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

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);

我想如果我更新状态,则根据此状态对更新组件做出反应。它应该如何工作?它应该如何工作?它可能有用,添加类别的项目不是父母或孩子,而是邻居 我的减速机

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
    }
}

状态不使用fixture,使用真正的api :)

【问题讨论】:

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


    【解决方案1】:

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

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

    代替

    state.list.push(addedCategory)
    

    【讨论】:

    • 也许问题是当我添加一个类别时,我会更新类别列表,就像接收类别时一样
    • 您在哪里添加类别到类别列表?请显示该代码
    • useEffect 对其 deps 进行浅层比较,因此,如果您仅向列表添加新类别(通过 push 方法),React 会将其解释为没有任何更改,因为对象(列表)仍然存在相同(有更多项目,但在一天结束时相同)
    • 非常感谢。你是对的。我使用了不可变的数据。只是为了测试,我用类别数组创建了固定装置,而不是克隆它。如果我使用克隆数组,一切正常。我将发布带有错误和评论的操作,可能对某人有用。感谢您提到不可变数据
    • 我在使用useSelector, useDispatch from react-redux时遇到这个问题
    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 2021-11-11
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多