【问题标题】:Formik & React Native: update parent valueFormik 和 React Native:更新父值
【发布时间】:2019-07-10 17:13:29
【问题描述】:

我有一个“虚拟”字段inspection.images,我需要使用setFieldValue 从子组件更新它,但它没有更新。我做错了什么?

parent-component.js

...
<Formik
    initialValues={{
        inspection: {images:[]}
    }}
    onSubmit={values => {
        console.log('form values: ');
        console.log(JSON.stringify(values, null, 2));
    }}
    render={({values, isValid, handleSubmit, setFieldValue, errors}) => (
        <View style={styles.screen}>
            <Walkthrough onChanged={this.onWalkThroughIndexChanged}>
                <InspectionForm navigation={this.props.navigation} values={values} setFieldValue={setFieldValue}
                                mediaObjectsHandler={this.mediaObjectsHandler.bind(this)}/>
            </Walkthrough>
        </View>
    )}
/>

child-component.js

...
export class InspectionForm extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            inspectionMedia: [],
            progress: 0,
        };
        props.setFieldValue('inspection.images', [...props.values.inspection.images, 'xxxx.png']);
    }

    render() {
        return (
            ...
        );
    };
}

【问题讨论】:

  • 您是否遇到任何错误?

标签: reactjs react-native state formik


【解决方案1】:

字段应与您希望更新的值的键匹配

看来您必须使用inspection 而不是inspection.images

【讨论】:

  • 你是对的!我不得不克隆对象并更改图像属性,然后调用this.props.setFieldValue('inspection', inspection);
  • 很高兴听到它有帮助?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
相关资源
最近更新 更多