reactjs - React:在父组件中获取子组件组件的状态
问题描述
我有这个容器,并且没有放置在同一级别。当我单击按钮(放置在父级上)时,如何获取表单的状态?
我创建了一个演示来解决我的问题。
https://codesandbox.io/s/kmqw47p8x7
class App extends React.Component {
constructor(props) {
super(props);
}
save = () => {
alert("how to get state of Form?");
//fire api call
};
render() {
return (
<div>
<Form />
<button onClick={this.save}>save</button>
</div>
);
}
}
- 我不想做的一件事是同步 onChange 事件的状态,因为在 Form 中可能有另一个 Form。
解决方案
推荐阅读
- c# - 解析时间跨度 1:00 和 01:00
- c# - 从磁盘显示大量图像的最佳方法
- python - 带有 Django 项目和 MS SQL 数据库的 GitLab CI 无法登录
- php - 将原始数据传递给邮递员时出现错误
- ruby-on-rails - ruby-elasticsearch 创建带有属性的索引查询不起作用
- spring-boot - 恰好有一次生产者变慢
- python - 如何在 Python 中生成具有许多零值的随机数列表?
- c - 文件问题中的错误输出但是当我用 1 个正确的输出替换 7 时
- sql-server - OPENDATASOURCE 在@DynamicSQL 中不起作用
- python - 在使用所有身份验证的 Django 中,我想为下面提供的以下信息制作一个没有 REST 框架的自定义 API