我有一个状态数组,如下所示:

const [semiForm, setSemiForm] = useState([
    {
        name: '',
        image: '',
        exist: false,
        mId: null,
        mDetails: [
            {
                line_type: '',
                stock_type: '',
                gl_categories: '',
                planner_number: '',
                planner_name: '',
                buyer_number: '',
                buyer_name: '',
                serial_no: '',
                lot_process_type: '',
                master_planning_family: '',
                level_1_category: '',
                level_2_category: '',
                level_3_category: '',
                level_4_category: '',
                category_code: '',
                unit_of_measure: '',
                secondary_uom: '',
                purchasing_uom: '',
                shipping_uom: '',
                production_uom: '',
                component_uom: ''
            },
        ],
        prepared_by: '',
        recipe_no: '',
        approved_by: '',
        recipe_date: '',
        shelf_life: '',
        recipe_type: '',
        section: '',
        approval_date: '',
        ty_final_product: '',
        usable_trim: '',
        ty_aft_chill: '',
        wastage: '',
        pri_ingr: [
            {
                item_code: '',
                item_description: '',
                start_weight: '',
                left_weight: '',
                actual_qty: '',
                uom: '',
                yield: '',
                strg_container: '',
                cook_vessel: '',
                prepared_by: ''
            },
        ],
        steps: [
            {
                description: '',
                image: ''
            },
        ],
        eqp_used: [
            {
                name: ''
            },
        ],
    }
])

我想根据父对象的"name"将一个新对象添加到"pri_ingr"数组中.

我try 使用下面的代码获取父对象的"名称",但即使在设置了状态之后,它也不起作用.

const addPriIngr = (name) => {
    console.log(name)
    // return
    let newPriIngrObj = {
        item_code: '',
        item_description: '',
        start_weight: '',
        left_weight: '',
        actual_qty: '',
        uom: '',
        yield: '',
        strg_container: '',
        cook_vessel: '',
        prepared_by: ''
    }

    semiForm.map((item, index) => {
        console.log(item.name);
    if (item.name === name) {
        return { ...item, pri_ingr: [...item.pri_ingr, newPriIngrObj] }
    }
    return {
        ...item
    }
    })
}

同样,我也将对另一个嵌套的对象数组使用它.

请帮我把这个整理一下.

推荐答案

您的映射代码看起来很正常.但是,当你拨打semiForm.map()的时候,你并没有真正改变什么.你必须以一种有用的方式消费semiForm.map()的结果.目前,您只呼叫.map(),而不对结果做任何操作.

与其做某事

semiForm.map((item, index) => {
    console.log(item.name);
    if (item.name === name) {
        return { ...item, pri_ingr: [...item.pri_ingr, newPriIngrObj] }
    }
    return {
        ...item
    }
});

你应该做的是

let semiFormResult = semiForm.map((item, index) => {
    console.log(item.name);
    if (item.name === name) {
        return { ...item, pri_ingr: [...item.pri_ingr, newPriIngrObj] }
    }
    return {
        ...item
    }
});
setSemiForm(semiFormResult);

这样,您将告诉Reaction使用.map()调用的结果作为semiForm的新状态.

我不知道更新状态是什么意思,但请注意,状态应该在调用.map()之后更新--而不是在传递给.map()的函数内部.

如果这不能解决您的问题,例如数据没有正确显示,我相信问题不在您更新到semiForm,而是在您的JSX绑定中的某个地方.也许绑定没有按照您期望的方式进行更新?

Reactjs相关问答推荐

无法覆盖MUI工具栏上的左右填充,除非使用!重要

使用Overpass API Endpoint计算 map 上的面积

如何将图像(在代码中称为自己)定位在蓝色圈内的点上?我正在使用material UI和Reaction

使用Reaction钩子窗体验证可选字段

XChaCha20-Poly1305的解密函数

使用REACT-RUTER-V6和ZUSTANDreact 中的身份验证

如何解决Reaction中遗留的上下文API错误?

REACTION-Easy-SORT返回.map错误

带有样式的工具提示导致无法向功能组件提供参考警告

状态更改时的rtk查询触发器

使用experial_useFormState将参数传递给服务器操作

如何在 JS 中绘制具有不同笔画宽度的样条线

损坏的图像 React useState

如何在 React Native 中更新跨屏幕呈现的上下文状态变量?

如何从 graphql/apollo 中的缓存中清除多个查询

在 Reactjs 中状态发生变化时渲染一个新组件而不替换旧组件

如何在 v6.4 中将 Auth0 提供程序与新的 createBrowsereRouter 一起使用?

链接的音译 Gatsby JS

点击提交后自动添加#

由于另一个子组件,如何在react 中渲染另一个子组件