更改useEffect的状态不会更改界面

时间:2019-09-11 12:50:49

标签: javascript reactjs react-hooks

我在反应中使用useState和useEffect钩子来渲染表单。但是,当我使用useEffect挂钩更新表单时。表单不会重新呈现。

from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator()

model.fit_generator(datagen.flow(x_train, y_train, batch_size=32),
                    steps_per_epoch=len(x_train) / 32, max_queue_size=50)

在useEffect挂钩中,api调用更新了表单,因此重新呈现仓库类型的select输入,但是select输入不会重新呈现。这可能是什么原因。

1 个答案:

答案 0 :(得分:2)

您还需要复制嵌套值:

{
        warehouseType: {
            elementType: 'select',
            elementConfig: {
                options: [
                    { value: '4', displayValue: 'Showroom' }
                ]
            },
            value: '1',
            validation: {},
            valid: true
        },

...
const updatedForm = { ...form };
                updatedForm.warehouseType.options = updatedWarehouseTypes;
                setForm(updatedForm);

您还错过了其中的elementConfigupdatedForm.warehouseTypes.elementConfig.options

但是复制嵌套值仍然是一个好主意。

const updatedForm = {
                    ...form, 
                    warehouseType: {...form.warehouseType, 
                          elementConfig: {...form.elementConfig,
                               options:updatedWarehouseTypes 
                    }}};

相关问题