Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 反应本机同步父和子状态_React Native - Fatal编程技术网

React native 反应本机同步父和子状态

React native 反应本机同步父和子状态,react-native,React Native,假设我有一个父组件和一个子组件,这两个组件的状态都有一个共同的“项”值。如何在父组件和子组件状态中同步“项”值?更具体地说,父组件可以是应用程序的一个页面,子组件可以是个性化输入。当我写入输入时,我希望页面状态被更新,当页面状态被外部因素(比如通过通知)更新时,我希望输入被更新 export default class Parent extends Component { constructor(props) { super(props); this.state={

假设我有一个父组件和一个子组件,这两个组件的状态都有一个共同的“项”值。
如何在父组件和子组件状态中同步“项”值?

更具体地说,父组件可以是应用程序的一个页面,子组件可以是个性化输入。当我写入输入时,我希望页面状态被更新,当页面状态被外部因素(比如通过通知)更新时,我希望输入被更新

export default class Parent extends Component {

  constructor(props) {
    super(props);
    this.state={
      item: 1,
    }
  }

  render() {
    return (
      <Child
        item = this.state.item
      />
    )
  }

  // I want any function of this type to automatically update
  // child state (and redraw child) without having to manually resynchronise
  modifyItem() {
    this.setState({ item: neValue })
  }

}

export default class Child extends Component {

  constructor(props) {
    super(props);
    this.state={
      item: this.props.item,
    }
  }

  // I want any function of this type to automatically update
  // parent state without having to manually resynchronise
  modifyItem() {
    this.setState({ item: neValue })
  }

} 
导出默认类父扩展组件{
建造师(道具){
超级(道具);
这个州={
项目:1,
}
}
render(){
返回(
)
}
//我希望任何这种类型的函数都能自动更新
//子状态(和重绘子状态),而无需手动重新同步
modifyItem(){
this.setState({item:neValue})
}
}
导出默认类子扩展组件{
建造师(道具){
超级(道具);
这个州={
项目:this.props.item,
}
}
//我希望任何这种类型的函数都能自动更新
//无需手动重新同步的父状态
modifyItem(){
this.setState({item:neValue})
}
} 
导出默认类父扩展组件{
建造师(道具){
超级(道具);
这个州={
项目:1,
}
}
onItemChange(项目){
this.setState({item});
}
render(){
返回(
)
}
//我希望任何这种类型的函数都能自动更新
//子状态(和重绘子状态),而无需手动重新同步
modifyItem(){
this.setState({item:neValue})
}
}
导出默认类子扩展组件{
建造师(道具){
超级(道具);
这个州={
项目:this.props.item,
}
}
//我希望任何这种类型的函数都能自动更新
//无需手动重新同步的父状态
modifyItem(){
this.setState({item:newValue})
如果(此.props.onItemChange){
this.props.onItemChange(newValue);
}
}
} 

谢谢您的回答,但这是一个手动技巧,这里没有自动同步。我不知道我所寻找的是否可行,但这不是我所寻找的答案。尝试使用Redux或Mobx进行集中存储并发送具有新价值的事件。那就行了
export default class Parent extends Component {

  constructor(props) {
    super(props);
    this.state={
      item: 1,
    }
  }
  onItemChange(item) {
    this.setState({ item });
  }

  render() {
    return (
      <Child
        item = this.state.item
        onItemChange={this.onItemChange.bind(this)}
      />
    )
  }

  // I want any function of this type to automatically update
  // child state (and redraw child) without having to manually resynchronise
  modifyItem() {
    this.setState({ item: neValue })
  }

}

export default class Child extends Component {

  constructor(props) {
    super(props);
    this.state={
      item: this.props.item,
    }
  }

  // I want any function of this type to automatically update
  // parent state without having to manually resynchronise
  modifyItem() {
    this.setState({ item: newValue })
    if (this.props.onItemChange) {
       this.props.onItemChange(newValue);
    }
  }

}