0
我有一個TourEditor類,它是兒童和TourEditor類的父級,它在提交時處理表單。我希望父tourEditor在孩子中調用函數updateFiles(files)時更新組件對象,但不幸的是,它只保存孩子的初始狀態並且不更新狀態。關於如何解決這個問題的任何想法?當孩子/父母更改狀態時更新父級
TourEditor.js
export default class TourEditor extends React.Component {
constructor(props){
super(props);
this.state = {"files": []};
this.updateFiles = this.updateFiles.bind(this);
}
componentDidMount() {
tourEditor({ component: this });
}
updateFiles(files) {
this.setState({
files: files
});
}
render() {
const { tour } = this.props;
return (<form
ref={ form => (this.tourEditorForm = form) }
onSubmit={ event => event.preventDefault() }
>
<FormGroup>
//....
</FormGroup>
</form>);
}
}
tourEditor.js
export default function tourEditor(options) {
console.log(options);
component = options.component;
validate();
}
我沒有看到你打電話'updateFiles',你應該包括它太 – Khang