【发布时间】:2017-08-31 23:18:12
【问题描述】:
您好,我目前正在研究 reactjs,但处于非常基础的水平。我试图解析从父母到孩子以及从孩子到父母的数据。父母对孩子工作正常,但孩子对父母我不能。
这是整个场景... 我有 3 个组件.. 应用程序、主页和用户。 从 App 组件到 Home 组件我想解析数据..它工作正常。 在 Home 组件中,我有一个 Input 字段。如果我写一些东西并单击按钮,那么输入值将解析为 App 组件。 App 是我的父母,Home 是孩子..
这里是代码... APP组件
constructor() {
super()
this.state = {
userName : ' '
};
}
changeUName(newName) {
this.setState({
userName: newName
});
console.log('user',newName); // Getting Undefined
}
render() {
return (
<div className="App">
<Home changeUser = {() => this.changeUName()} />
</div>
);
}
子组件用户
constructor(props) {
super();
this.state = {
userName: ''
};
}
changeUName(event) {
this.setState({
userName: event.target.value
});
}
nameChange() {
console.log(this.state.userName) // If I write "Test" in input field. I will get the value here.
this.props.changeUser(this.state.userName); // Once I parse this value, I am getting undefined..
}
render() {
return(
<div>
<h1> HOME Page</h1>
Name : <input type="text" value={this.state.userName} onChange={(event) => this.changeUName(event)} ref="uName"/><br/>
<button onClick={() => this.nameChange()}>Click</button>
</div>
)
}
我不知道哪里出错了。请指导我。提前谢谢..
【问题讨论】:
-
谢谢@Shubham。我看过这个帖子。从昨天开始尝试了很多,但仍然显示未定义的值。
标签: reactjs react-redux