简体   繁体   English

如何在 React 中单击按钮时访问另一个组件的方法?

[英]How to access method of another component on button click in React?

I have a button component that when clicked, should call a method of another component which will increment a value shown to the user.我有一个按钮组件,当单击它时,应该调用另一个组件的方法,该方法将增加一个向用户显示的值。 However, when I try to set the button to call that method on click, I always get the error, "Cannot read property of 'incrementState' of undefined'. How can I call this method when the button is clicked?但是,当我尝试将按钮设置为在单击时调用该方法时,我总是收到错误消息“无法读取未定义的'incrementState'的属性”。单击按钮时如何调用此方法?

class IncreaseDiceButton extends React.Component {
      render () {
          return (<button onClick={NumDice.incrementState}>
                      Increase Number Of Dice
                  </button>); 
      }
};
class NumDice extends React.Component
{ 

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

  incrementState = () =>{
    var myNewState = this.state.mystate +1;
    this.setState({mystate: myNewState});
  };

  decrementState= () =>{
    var myNewState = this.state.mystate -1;
    if(myNewState > 0){
      this.setState({mystate: myNewState});
    }
  }
  render () {
    return ( <h2>
               {this.state.mystate}
             </h2>
           );  
  }
};

What you have in mind is an anti-pattern in React.你想到的是 React 中的反模式。 You should form a hierarchy, mount both components on the same screen, then lifting the state up.您应该形成一个层次结构,将两个组件安装在同一屏幕上,然后将 state 向上提起。

To think about how to redesign, first ask yourself what are the usage for each and every React Component, the behaviour and so on, and try to simplify them as much as possible.要考虑如何重新设计,首先要问自己每个 React Component 的用途、行为等,并尽量简化它们。

NumDice is a React Component that rendering the latest count, so we can refactor it into below NumDice是一个渲染最新计数的 React 组件,因此我们可以将其重构为下面

class NumDice extends React.Component
{
  render () {
    return (
      <h2>
        {this.props.currentDice}
      </h2>
    );  
  }
};

And IncreaseDiceButton component is to increase the Dice right?IncreaseDiceButton组件就是增加Dice吧? So we can refactor into below所以我们可以重构如下

class IncreaseDiceButton extends React.Component {
  render () {
    return (
      <button onClick={this.props.increaseDice}>
        Increase Number Of Dice
      </button>
    ); 
  }
};

And we need a "parent" component to manage the state, and the function to update the state, so below is an example我们需要一个“父”组件来管理 state 和 function 来更新 state,所以下面是一个示例

class ParentComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {mystate: 1};
  }

  incrementState = () =>{
    var myNewState = this.state.mystate +1;
    this.setState({mystate: myNewState});
  };

  decrementState= () =>{
    var myNewState = this.state.mystate -1;
    if(myNewState > 0){
      this.setState({mystate: myNewState});
    }
  }

  render() {
    return (
      <>
        <IncreaseDiceButton increaseDice={this.incrementState} />
        <NumDice currentDice={this.state.mystate} />
      </>
    )
  }
}

And to put all puzzles together, below will be the way to construct it并将所有谜题放在一起,下面将是构建它的方法

class ParentComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {mystate: 1};
  }

  incrementState = () =>{
    var myNewState = this.state.mystate +1;
    this.setState({mystate: myNewState});
  };

  decrementState= () =>{
    var myNewState = this.state.mystate -1;
    if(myNewState > 0){
      this.setState({mystate: myNewState});
    }
  }

  render() {
    return (
      <>
        <IncreaseDiceButton increaseDice={this.incrementState} />
        <NumDice currentDice={this.state.mystate} />
      </>
    )
  }
}

class IncreaseDiceButton extends React.Component {
  render () {
    return (
      <button onClick={this.props.increaseDice}>
        Increase Number Of Dice
      </button>
    ); 
  }
};

Please note on the usage on how to pass the function reference by props .请注意如何通过props传递 function 参考的用法。 If you're serious about React, you do have to master what props and state is.如果你对 React 很认真,你必须掌握什么是propsstate Welcome to React:)欢迎来到反应:)

Generally, you're going to want to "lift" state up to a common ancestor.一般来说,你会想要将 state 提升到一个共同的祖先。 I took your code below and lifted state to a common "App" ancestor.我在下面获取了您的代码,并将 state 提升为一个共同的“App”祖先。

class IncreaseDiceButton extends React.Component {
  render () {
    return (
      <button onClick={props.incrementState}>
        Increase Number Of Dice
      </button>
    );
  }
};
class NumDice extends React.Component { 
  render() {
    return (
      <h2>
        {props.mystate}
      </h2>
    );  
  }
};
class App extends React.Component {
  constructor () {
    super();
    this.state = { mystate: 1 }; 
  }

  incrementState = () => {
    var myNewState = this.state.mystate +1;
    this.setState({mystate: myNewState});
  };

  decrementState = () => {
    var myNewState = this.state.mystate -1;
    if(myNewState > 0){
      this.setState({mystate: myNewState});
    }
  }

  render() {
    return (
      <NumDice mystate={this.state.mystate} />
      <IncreaseDiceButton incrementState={incrementState} />
    )
  }
}

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

相关问题 如何在单击一次按钮时呈现另一个反应组件? (功能组件) - How to render another react component on a single button click? (Functional Component) 如何通过在另一个组件中单击按钮在一个 React 组件中调用 function - How to call a function in one React component by button click in another component 单击按钮时在另一个组件中附加一个 React 组件 - Append a React component in another on button click 在 React 的另一个组件中单击按钮的事件侦听器 - Event Listener for button click in another component in React 指向另一个组件的按钮上的单击方法 - on-click method on a button that directs to another component 在 React 中单击来自不同组件的按钮时触发另一个组件中的函数 - Trigger a function in another component on click a button from a different component in React 我们如何在另一个组件中单击按钮时更改反应组件中元素的 state? - How do we change the state of element in a react component on button click in another component? 如何在React中的另一个按钮处单击按钮? - How to click at button at the another button in React? 如何在单击按钮时打印 React 组件? - How to print React component on click of a button? 如何从按钮单击加载组件? - How to load a component from a button click in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM