开发者问题收集

为什么这个组件中没有设置我的状态

2019-10-05
48

正在开发我的 React 技能,构建一个卡片匹配游戏。一共有 16 张卡片,你每次翻转 2 张。如果匹配,它们会保持翻转状态并显示正面,如果不匹配,它们会翻转回来显示卡片的背面。

这个项目还没有完成,需要一步一步地完成。我已经使用 JSX 图像中对状态的动态引用添加了卡片背面(初始状态)(在 render->return 中)。它们按预期面朝下加载。onClick 正确地从事件中获取了 id,并且我尝试 setState 以在用户单击时显示卡片正面(通过“selected”方法)。当我使用 console.log 进行测试时,会从事件中记录正确的 id,但是当我使用 console.log 记录状态时,它显示未定义。

我做错了什么?

import React, {Component} from 'react';
import './Game.css';
import cardBack from '../../Images/back.png';
import j from '../../Images/j.jpg';
import q from '../../Images/q.jpg';
import k from '../../Images/k.jpg';
import a from '../../Images/a.png';



class Game extends Component {

    state = {
        card0: cardBack, 
        card1: cardBack, 
        card2: cardBack, 
        card3: cardBack, 
        card4: cardBack, 
        card5: cardBack, 
        card6: cardBack, 
        card7: cardBack, 
        card8: cardBack, 
        card9: cardBack, 
        card10: cardBack, 
        card11: cardBack, 
        card12: cardBack, 
        card13: cardBack, 
        card14: cardBack, 
        card15: cardBack, 
    }

    selected = (event) => {
        let id = event.currentTarget.id;
        console.log(id);
        this.setState({id, q});
        console.log(this.state[id]);
    }


    render() {




        return (
            <div id="gameBox">
                <img src={this.state.card0} id="card0" className="card" onClick={this.selected}/>
                <img src={this.state.card1} id="card1" className="card" onClick={this.selected}/>
                <img src={this.state.card2} id="card2" className="card" onClick={this.selected}/>
                <img src={this.state.card3} id="card3" className="card" onClick={this.selected}/>
                <img src={this.state.card4} id="card4" className="card" onClick={this.selected}/>
                <img src={this.state.card5} id="card5" className="card" onClick={this.selected}/>
                <img src={this.state.card6} id="card6" className="card" onClick={this.selected}/>
                <img src={this.state.card7} id="card7" className="card" onClick={this.selected}/>
                <img src={this.state.card8} id="card8" className="card" onClick={this.selected}/>
                <img src={this.state.card9} id="card9" className="card" onClick={this.selected}/>
                <img src={this.state.card10} id="card10" className="card" onClick={this.selected}/>
                <img src={this.state.card11} id="card11" className="card" onClick={this.selected}/>
                <img src={this.state.card12} id="card12" className="card" onClick={this.selected}/>
                <img src={this.state.card13} id="card13" className="card" onClick={this.selected}/>
                <img src={this.state.card14} id="card14" className="card" onClick={this.selected}/>
                <img src={this.state.card15} id="card15" className="card" onClick={this.selected}/>
            </div>
        );
    }

}

export default Game;
2个回答
this.setState( { [id] : q } );

尝试一下,希望效果良好。

Abhishek-Saini
2019-10-05

问题是你还没有启动 id 的状态

import React, {Component} from 'react';
import './Game.css';
import cardBack from '../../Images/back.png';
import j from '../../Images/j.jpg';
import q from '../../Images/q.jpg';
import k from '../../Images/k.jpg';
import a from '../../Images/a.png';



class Game extends Component {

    state = {
        card0: cardBack, 
        card1: cardBack, 
        card2: cardBack, 
        card3: cardBack, 
        card4: cardBack, 
        card5: cardBack, 
        card6: cardBack, 
        card7: cardBack, 
        card8: cardBack, 
        card9: cardBack, 
        card10: cardBack, 
        card11: cardBack, 
        card12: cardBack, 
        card13: cardBack, 
        card14: cardBack, 
        card15: cardBack, 
        id:null // initiating the state
    }

    selected = (event) => {
        let id = event.currentTarget.id;
        console.log(id);

        // Changed code
        this.setState({
           id
        }, () => { console.log(this.state.id)   })


    }


    render() {




        return (
            <div id="gameBox">
                <img src={this.state.card0} id="card0" className="card" onClick={this.selected}/>
                <img src={this.state.card1} id="card1" className="card" onClick={this.selected}/>
                <img src={this.state.card2} id="card2" className="card" onClick={this.selected}/>
                <img src={this.state.card3} id="card3" className="card" onClick={this.selected}/>
                <img src={this.state.card4} id="card4" className="card" onClick={this.selected}/>
                <img src={this.state.card5} id="card5" className="card" onClick={this.selected}/>
                <img src={this.state.card6} id="card6" className="card" onClick={this.selected}/>
                <img src={this.state.card7} id="card7" className="card" onClick={this.selected}/>
                <img src={this.state.card8} id="card8" className="card" onClick={this.selected}/>
                <img src={this.state.card9} id="card9" className="card" onClick={this.selected}/>
                <img src={this.state.card10} id="card10" className="card" onClick={this.selected}/>
                <img src={this.state.card11} id="card11" className="card" onClick={this.selected}/>
                <img src={this.state.card12} id="card12" className="card" onClick={this.selected}/>
                <img src={this.state.card13} id="card13" className="card" onClick={this.selected}/>
                <img src={this.state.card14} id="card14" className="card" onClick={this.selected}/>
                <img src={this.state.card15} id="card15" className="card" onClick={this.selected}/>
            </div>
        );
    }

}

export default Game;
Nane
2019-10-05