简体   繁体   English

是否可以在React Native中从其无状态组件子级更改父级的状态?

[英]Is it possible to change state of a parent from its stateless component child in React Native?

In my App.js, I have initialized some states like so, 在我的App.js中,我已经初始化了一些状态,

export default class App extends React.Component {
  constructor(){
    super();
     this.state = {
            myfirststate: '',
            mysecondstate: '',
     }    
    }
    ...
   }

Now I have a ChildComponent.js which is stateless and is like this which has a button and when clicked, changes the state of myfirststate from null to hello . 现在,我有一个ChildComponent.js,它是无状态的,就像这样,它有一个按钮,当单击时,将myfirststate的状态从null更改为hello

 export default class ChildComponent extends React.Component {
   render() {
    return (
      <View>
        <Button
          title="clickme"
          onPress={() => 
             this.setState({myfirststate: "hello"})
        />
      </View>
      );
     }
    }

Is it possible to do something like this? 有可能做这样的事情吗? I always thought I had to pass the value of states as arguements in a function which was passed from parent component to child like this instead. 我一直以为我必须将状态的值作为论据传递给一个函数,该函数像这样从父组件传递给子组件。

App.js App.js

 export default class App extends React.Component {
  constructor(){
   super();
    this.state = {
        myfirststate: '',
        mysecondstate: '',
     }    
   }

 functiontopassvalues(values) {
    this.setState({
        myfirststate: values
    })
   }
   <ChildComponent functiontopassvalues={this.functiontopassvalues} />
  }

And in ChildComponent.js I would do something like this ChildComponent.js我会做这样的事情

    export default class ChildComponent extends React.Component {
      render() {
        return (
         <View>
           <Button
            title="clickme"
             onPress={() => 
             {this.props.functiontopassvalues("hello")}
        />
      </View>
      );
     }
    }

I hope I am making sense, please let me know what is the correct method to do it and why it is so. 我希望我有道理,请让我知道正确的方法以及这样做的正确性。 Also, does it apply to both react native and react.js? 另外,它是否适用于react native和react.js?

Your approach is correct, but your child component is aware what value needs to be passed to parent, technically it is correct, but it's not scalable. 您的方法是正确的,但是您的子级组件知道需要将什么值传递给父级,从技术上讲,这是正确的,但它不可扩展。 You can pass an onPress handle to child component and in parent set whatever value you want in parent state. 您可以将onPress句柄传递给子组件,并在父集合中传递您希望在父状态下使用的任何值。

Consider following example 考虑以下示例

export default class App extends React.Component {
 constructor(){
  super();
  this.state = {
    myfirststate: '',
    mysecondstate: '',
  }    
 }

 functiontopassvalues= (values) => {
  this.setState({
    myfirststate: values
  });
 }
 <ChildComponent title="click me" onPress={() => this.functiontopassvalues('hello')} />
}

Child component can be made scalable by a few changes 可以通过一些更改使子组件可扩展

export default class ChildComponent extends React.Component {
  render() {
    return (
     <View>
       <Button
         title={this.props.title}
         onPress={this.props.onPress}
       />
     </View>
   );
 }
}

It is always better to use any state management tools as specified by @Sam. 最好使用@Sam指定的任何状态管理工具。 They are pretty helpful. 他们非常有帮助。

Your way of solving the problem is a fair and correct, but it isn't necessarily scalable, and you are correct in that you can't alter state of a parent in a child. 您解决问题的方式是公平正确的,但不一定可以扩展,并且您无法更改父母在孩子中的状态是正确的。

I would recommend checking out tools like Redux , MobX or Apollo Link State that all try to solve the eternal problem of state management. 我建议您检查一下ReduxMobXApollo Link State之类的工具,它们都试图解决状态管理的永恒问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM