Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改最终形式中嵌套字段的值_Javascript_Reactjs_React Final Form - Fatal编程技术网

Javascript 更改最终形式中嵌套字段的值

Javascript 更改最终形式中嵌套字段的值,javascript,reactjs,react-final-form,Javascript,Reactjs,React Final Form,我们正在使用React最终表单库,希望更改用户选择的嵌套字段的值 它基本上是地址形式,在选择郊区时,我想更改邮政编码和状态。 地址也是表单中的嵌套对象。 我的数据集看起来像:- { name: 'xyzx', address: { suburb: 'xx', }, } 我在通过变异人 export const changeValueMutator: Mutator = ([name]: string[], state: MutableState, { changeValue

我们正在使用React最终表单库,希望更改用户选择的嵌套字段的值

它基本上是地址形式,在选择
郊区
时,我想更改
邮政编码
状态
。 地址也是表单中的嵌套对象。 我的数据集看起来像:-

{
  name: 'xyzx',
  address: {
    suburb: 'xx',
  },
}
我在通过变异人

export const changeValueMutator: Mutator = ([name]: string[], state: MutableState, { changeValue }: Tools) => {
  console.log(state, name);
  changeValue(state, name, value => (value));
};
并称之为

this.props.changeValue('address.suburb', item.suburb);
如果你愿意,你可以试试这个

state = {
  json : {
    name: 'xyzx',
    address: {
    suburb: 'xx',
  },
}

this.state.json.address.suburb = "yy";
this.setState({json : this.state.json});

最终形式中的嵌套字段用点表示法引用


我从库中的一个测试用例中找到了答案

我修改了我的mutator方法,如下所示(将newValue作为第一个参数数组中的第二个参数)。


好的答案有更好的解释
<Field
    name="address.suburb"
    component="input"
    type="text"
    placeholder="Suburb"
/>
export const changeValueMutator: Mutator =
  ([name, newValue]: string[], state: MutableState, { changeValue }: Tools) => {
  console.log(state, name);
  changeValue(state, name, value => (newValue));
};