[英]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 很认真,你必须掌握什么是
props
和state
。 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.