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

问题描述 投票:2回答:2
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>
        );
    }
}

我希望能够点击图像是LeagueBadge组件,并在兄弟的LeagueInfo中增加amountOfPlayers的值。我已经google了反应兄弟姐妹的通信,只找到了带输入标签和onChange的例子,但在这里我想要img标签或按钮和onClick。

javascript reactjs onclick components increment
2个回答
2
投票

你可以将amountOfPlayers的状态提升到你的<Leauge />组件中,这样: - 可以从<LeagueBadge />触发更新 - 并且,该状态可以传递给你的<LeagueInfo />组件

这将允许您根据需要共享和同步<LeagueInfo /><LeagueBadge />兄弟姐妹之间的状态。

为此,您需要向onClick添加一个<LeagueBadge />回调函数,该函数在单击img元素时触发。在<Leauge />渲染方法中,您可以提供在amountOfPlayers中增加<Leauge />状态的逻辑。当amountOfPlayers增加,并且setState被调用(在<Leauge />中)时,这将导致你的<Leauge />组件重新呈现自己(和孩子/兄弟姐妹)。因为amountOfPlayers的更新值作为prop传递给<LeagueInfo />组件,所以这个更新的值将在<LeagueInfo />-中有效地实现你所追求的目标。

这样的事可能适合你:

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>
        );
    }
}

2
投票

保持你的状态在联盟组件中并传递负责的功能将其更改为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}/>
    );
}

在Info组件中使用this.props.amountOfPlayers

© www.soinside.com 2019 - 2024. All rights reserved.