我从我的表单组件中获取数据,并尝试使用此数据设置我的应用程序组件的状态.
但是,state.data是一个空对象,不会更新数据.我在设置之前控制记录模型数据以检查它是否存在.它们是模型中的数据.
import React, { Component, Fragment } from "react";
import Form from "../components/Form";
import product from "./product.json";
class App extends Component {
constructor() {
super();
this.state = {
data: {}
};
}
onSubmit = (model) => {
console.log("Outer", model);
this.setState({
data: model
});
console.log("Form: ", this.state);
}
render() {
const fields = product.fields;
return (
<Fragment>
<div>Header</div>
<Form
model={fields}
onSubmit={(model) => {this.onSubmit(model);}}
/>
<div>Footer</div>
</Fragment>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud) reactjs ×1