简体   繁体   English

如何在单击时从一个兄弟组件中增加一个兄弟组件中的值?

[英]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.

相关问题 如何从 React 中的另一个兄弟组件触发兄弟组件 function? - How to trigger sibling component function from another sibling component in React? 我如何共享一个兄弟姐妹的功能到另一个兄弟姐妹的组件 - how can i share function of one sibling to another sibling component 如何通过反应中兄弟组件上的单击事件将图像加载到另一个组件中 - how to load an image in another component by click event on sibling component in react 在 angular 8 中,我们如何将数据从一个组件发送到另一个同级组件? - How we send data from one component to another sibling component in angular 8? 如何从 React JS 中的另一个组件更改同级组件的事件 - How to change sibling component on event from another component in React JS 如何从一个组件到另一个组件获取价值? - How to get value from one component to another? 如何将价值从一个组件发送到另一个组件 - How to send value from one component, to another 如何根据兄弟值增加 object 值? - How to increment object value based on sibling value? 如何在 REACT 中从另一个同级或导入的组件更新同级组件的状态 - How to update the state of a sibling component from another sibling or imported component in REACT 如何防止一个组件来自另一个组件的点击事件? - How to prevent click event for one component from another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM