javascript - ReactJS passing input data to next screen/component
问题描述
I've looked up whole google for my answer (haha). What I am trying to achieve:
My fist screen is a form with some input fields which store the input to state. By clicking "submit" the user comes to the next page/screen, where the previous input is supposed to show up. I want to show only the input data as a p-tag, not as an input tag anymore.
How do I achieve this? I am bloody new to reactjs.
I've thought about multiple solutions:
- store the input data to an external JSON (which seems pretty complicated)
- pass the data as props, but I am not aware of how I would do this
Thanks so far!
解决方案
我将假设您的第二个屏幕与您的第一个屏幕具有相同的父组件,如下所示:
render() {
return (
<div>
{this.state.showFirstScreen && <FirstScreen />}
{this.state.showSecondcreen && <Secondcreen />}
</div>
)
}
如果是这种情况,您的FirstScreen
组件可以接受一个称为类似的道具onSubmit
- 您说您已经存储了FirstScreen
状态中的输入值,所以当FirstScreen
完成时,您可以调用this.props.onSubmit(this.state)
并将状态发送到您的父组件,如只要你在父组件中定义了回调函数:
{this.state.showFirstScreen && <FirstScreen onSubmit={this.onFirstScreenSubmit} />}
然后,您可以将第一个屏幕的状态保存到父组件的状态中,并将该部分状态传递给SecondScreen
[编辑] 这是一个非常粗略的例子:https ://jsfiddle.net/df4s9ey8/
推荐阅读
- tensorflow - 我可以使用`tf.contrib.seq2seq.dynamic_decode`来替换encoder-decoder框架中的函数`tf.nn.dynamic_rnn`吗?
- debugging - IBM System i - 用于 RDi 调试的调试服务器不起作用
- class - Tensorflow中连体模型中子类的共享权重
- coq - Coq 影响分析
- sql - 在 Redshift sql 上对时间间隔进行分组
- mysql - MySQL 服务器:仅将一些数据库移动到另一个位置
- oracle - 与列匹配后需要获取给定字符串中的最大值
- python-2.7 - 使用 Google Cloud Platform 的简单任务队列:Google PubSub 的问题
- ios - 作为 TableView 数据源的 Swift 通用枚举
- javascript - 从 url 中删除特定字符串并重定向