Reactjs Can';在react native中将状态值从子组件传递到父组件
我想在父组件中使用子组件内部的状态。以下是我的组件的设置方式: 儿童:Reactjs Can';在react native中将状态值从子组件传递到父组件,reactjs,react-native,Reactjs,React Native,我想在父组件中使用子组件内部的状态。以下是我的组件的设置方式: 儿童: export default class Child extends React.PureComponent { constructor() { this.state = { value: "123", }; } updateValue(data){ this.props.updateValue(data); } componentWillMount(){ this.up
export default class Child extends React.PureComponent {
constructor() {
this.state = {
value: "123",
};
}
updateValue(data){
this.props.updateValue(data);
}
componentWillMount(){
this.updateValue(this.state.value);
}
}
家长:
export default class Parent extends Component {
constructor() {
super(props)
this.state = {
newValue: ""
};
}
updateState (data) {
this.setState(newValue: data);
}
render() {
return(
<Child updateValue={this.updateState.bind(this)}/>
)
}
}
导出默认类父扩展组件{
构造函数(){
超级(道具)
this.state={
新值:“
};
}
房地产(数据){
此.setState(新值:数据);
}
render(){
返回(
)
}
}
然而,它似乎不起作用,给了我一个错误。我做错什么了吗?记住
this.setState
需要一个参数。您传递了一个未存储在数据结构中的key:value对
this.setState(newValue: data);
应该是
this.setState({newValue: data});
错误会说什么?
this.setState(newValue:data)
不是有效的对象-应该是this.setState({newValue:data})代码>它准确地告诉你出了什么问题。字面上