这是父组件中input所在
<FormItem {...formItemLayout} label="父级ID" >
<SelectParentId id="pIdselect" onChange="onchangeFn" {...getFieldProps('pIdselect')} />
<Input hidden id="pid" {...getFieldProps('pId')} />
</FormItem>
这是子组件,获取相应的数据
constructor(props) {
super(props);
this.state = {
options: [],
}
}
conponentDideMount() {
}
/**
* 当选择框里内容变化时查询数据
* @param e
*/
handleChange(value) {
let options;
if ((value !== null) && (value.length < 2)) {
printLog("value.length------------------" + value.length);
} else {
printLog("value.length>2:" + value.length);
callAutocompconsteChoiceFn(value)
.then((res) => {
const datas = res.data;
printLog("datas================"+JSON.stringify(datas));
options = datas.map((item) => {
const ids = item.id.toString();
const dictTypeSstrings = item.dictType.toString();
const propKeyStrings = item.propKey.toString();
const labelKeyStrings = item.labelKey.toString();
const joints = `${dictTypeSstrings}-${propKeyStrings}-${labelKeyStrings}-${ids}`;
return <Option key={ids} value={joints}>{joints}</Option>;
});
this.setState({ options });
});
}
}
render() {
const self = this;
return (
<Select
combobox
style={{ width: '100%' }}
onChange={(options) => this.handleChange(options)}
filterOption={false}
placeholder="父级ID"
>
{this.state.options}
</Select>
)
}
现在我无法拿到子组件中的数据到父组件中,即是在父组件中红无法通过 pId: getFieldValue('pId'),
拿到数据,如何才能将子组件的数据暴露给父组件以让其获取到呢
React 设计中,不推荐父组件直接访问或操作子组件的。这样违背单向数据的思想,而且无法保证数据同源。
通常是子组件在完成操作后,通过触发一个props传递过来的的callback,在父级获取。
况且父子组件传值,最好的放法就是通过props。