简体   繁体   English

React 动态设置多个状态

[英]React setting multiple states dynamically

Im learning React and currently playing with passing data around.我正在学习 React,目前正在玩传递数据。

I have info for 5 cards stored in state like this:我有 5 张卡片的信息存储在这样的状态:

state = {
    cards: [
        { id: 1, name: "p1", value: "Prize 1", imageSrc: "/path/to/image", bgColor: { background: "#FF6384", border: "4px solid #FF6384" } },
        { id: 2, name: "p2", value: "Prize 2", imageSrc: "/path/to/image", bgColor: { background: "#4BC0C0", border: "4px solid #4BC0C0" } },
        { id: 3, name: "p3", value: "Prize 3", imageSrc: "/path/to/image", bgColor: { background: "#FFCE56", border: "4px solid #FFCE56" }},
        { id: 4, name: "p4", value: "Prize 4", imageSrc: "/path/to/image", bgColor: { background: "#67bd42", border: "4px solid #67bd42" } },
        { id: 5, name: "p5", value: "Prize 5", imageSrc: "/path/to/image", bgColor: { background: "#c931f7", border: "4px solid #c931f7" }}
    ],
    flipped: null,
   };

I am displaying them with an iteration, like this:我用迭代显示它们,如下所示:

<div className="prize-cards-inner">
                {this.state.cards.map((card) => {
                    return (
                        <section key={card.id} className="prize-card-container">
                            <div
                                className={`prize-card ${
                                    this.state.flipped === card ? "flipped" : ""
                                    }`}
                                onClick={() => this.clickHandler(card)}
                            >
                                <div className="front">
                                    <div className="card-name-div" style={ card.bgColor }>
                                        {card.value}
                                    </div>
                                    <div className="prize-image-div" >
                                        <img className="prize-image"  src={card.imageSrc} alt="test" /> 
                                        
                                    </div>
                                    <div className="slot-name-div" style={card.bgColor}>
                                       <p> {card.name}</p>
                                    </div>
                                </div>
                                <div className="back">Prize</div>
                            </div>
                        </section>
                    );
                })}
                
            </div>

and handling card clicks with this:并通过以下方式处理卡点击:

    clickHandler = (card) => {
    if (this.state.flipped) {
        return;
    }
    this.setState({
        flipped: card,
        ***Set another state for all other cards (how do I say "set the state of all cards that aren't this card?")***
    });

};

I would like to set the state for all other cards aside from the clicked card at the same time I set the state for the clicked card.我想在设置被点击卡的状态的同时,为除了被点击的卡之外的所有其他卡设置状态。 I have no clue how to do what I want, so I have marked the part I am unsure about with asterisks.我不知道如何做我想做的事,所以我用星号标记了我不确定的部分。 Thanks in advance for your time.在此先感谢您的时间。

I think you are looking for this:我想你正在寻找这个:

const clickHandler = (card) => {
 if (this.state.flipped) {
   return;
 }

 this.setState((state) => ({
   flipped: card,
   cards: state.cards.map((c) => ({
     ...c, someOtherState: 'some value',
   }))
 });
};

I'd recommend you start using functional components with React hooks right away.我建议您立即开始使用带有 React 钩子的函数式组件。 They provide much nicer developer experience它们提供了更好的开发人员体验

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM