[英]How to increment a value in one sibling component from another on click?
function LeagueBadge(props) {
return (
<img src={props.badgeUrl} alt="missing alt text" />
);
}
class LeagueInfo extends Component {
constructor(props) {
super(props);
this.state = {
amountOfPlayers: null,
rpPerSecond: null,
rpCost: null,
};
}
render() {
return (
<div>
<h4>{this.props.name} players: {this.props.amountOfPlayers}</h4>
<h4>RP per second: {this.props.rpPerSecond}</h4>
<h4>RP cost: {this.props.rpCost}</h4>
</div>
);
}
}
class League extends Component {
render() {
return (
<div>
<LeagueBadge badgeUrl={this.props.badge} />
<LeagueInfo name={this.props.name} />
</div>
);
}
}
class App extends Component {
render() {
return (
<div className="App">
<h1>Players</h1>
<League name="Bronze" badge={ require('./bronze.png') }></League>
<League name="Silver" badge={ require('./silver.png') }></League>
<League name="Gold" badge={ require('./gold.png') }></League>
<League name="Platinum" badge={ require('./platinum.png') }></League>
<League name="Diamond" badge={ require('./diamond.png') }></League>
<League name="Master" badge={ require('./master.png') }></League>
<League name="Challenger" badge={ require('./challenger.png') }></League>
</div>
);
}
}
I want to be able to click at the image which is the LeagueBadge component and increment the value of amountOfPlayers in their sibling LeagueInfo. 我希望能够单击是LeagueBadge组件的图像,并在其同级LeagueInfo中增加amountOfPlayers的值。 I've googled react siblings communication already and only found examples with input tag and onChange but here I want img tag or button and onClick.
我已经用谷歌搜索了兄弟姐妹之间的通信,并且只找到了带有输入标签和onChange的示例,但是在这里我想要img标签或按钮和onClick。
You could lift the state for amountOfPlayers
up into your <Leauge />
component, so that: - updates can be triggered from <LeagueBadge />
- and, that state can be passed down to your <LeagueInfo />
component 您可以将
amountOfPlayers
的状态提升到您的<Leauge />
组件中,以便:-可以从<LeagueBadge />
触发更新-并且可以将该状态传递给您的<LeagueInfo />
组件
This would allow you to share and synchronize state between the <LeagueInfo />
and <LeagueBadge />
siblings as you require. 这将允许您根据需要在
<LeagueInfo />
和<LeagueBadge />
兄弟姐妹之间共享和同步状态。
To do this, you would need to add an onClick
callback to <LeagueBadge />
which is fired when the img
element is clicked. 为此,您需要向
<LeagueBadge />
添加一个onClick
回调,该回调在单击img
元素时触发。 In the <Leauge />
render method, you could provide the logic that increments the amountOfPlayers
state in <Leauge />
. 在
<Leauge />
渲染方法中,您可以提供增加<Leauge />
的amountOfPlayers
状态的逻辑。 When the amountOfPlayers
is incremented, and setState
is called (in <Leauge />
), this would in turn cause your <Leauge />
component to re-render itself (and children/siblings). 当
amountOfPlayers
增加时,调用setState
(在<Leauge />
),这又将导致您的<Leauge />
组件重新呈现自身(以及子级/同级)。 Because the updated value for amountOfPlayers
is passed as a prop to <LeagueInfo />
component, this updated value would be rendered in <LeagueInfo />
- effectively achieving what you're after. 因为
amountOfPlayers
的更新值作为道具传递给<LeagueInfo />
组件,所以此更新值将在<LeagueInfo />
呈现-有效地实现了您所追求的目标。
Something like this might work for you: 这样的事情可能适合您:
class LeagueBadge extends Component {
render() {
// Add props.onClick callback to trigger click event in <League />
// component
return (
<img src={this.props.badgeUrl} alt="missing alt text"
onClick={() => this.props.onClick()} />
);
}
}
class LeagueInfo extends Component {
constructor(props) {
super(props);
this.state = {
// amountOfPlayers: null, // This is not needed, as it's supplied by props
rpPerSecond: null,
rpCost: null,
};
}
render() {
return (
<div>
<h4>{this.props.name} players: {this.props.amountOfPlayers}</h4>
<h4>RP per second: {this.props.rpPerSecond}</h4>
<h4>RP cost: {this.props.rpCost}</h4>
</div>
);
}
}
class League extends Component {
componentWillMount() {
this.setState({
amountOfPlayers : 0
})
}
render() {
// Locally defined function that increments amountOfPlayers and
// updates state
const incrementAmountOfPlayers = () => {
this.setState({ amountOfPlayers :
this.state.amountOfPlayers + 1 })
}
return (
<div>
<LeagueBadge badgeUrl={this.props.badge}
onClick={ () => incrementAmountOfPlayers() } />
<LeagueInfo name={this.props.name} amountOfPlayers={ this.state.amountOfPlayers } />
</div>
);
}
}
Keep your state in the league component and pass the function responsible to change it to LeagueBadge like: 将您的状态保留在League组件中,并传递负责将其更改为LeagueBadge的功能,例如:
class League extends Component {
constructor(props) {
super(props);
this.state = {
amountOfPlayers: null,
};
}
handleClick = () => {
this.setState(prevState => {
return {amountOfPlayers: prevState.amountOfPlayers + 1}
})
}
render() {
return (
<div>
<LeagueBadge badgeUrl={this.props.badge} incrementPlayers={this.handleClick}/>
<LeagueInfo name={this.props.name} amountOfPlayers={this.state.amountOfPlayers}/>
</div>
);
}
}
function LeagueBadge(props) {
return (
<img src={props.badgeUrl} alt="missing alt text" onClick={this.props.incrementPlayers}/>
);
}
use this.props.amountOfPlayers
in your Info component. 在您的Info组件中使用
this.props.amountOfPlayers
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.