简体   繁体   English

布尔无法在jsx中识别?

[英]boolean is not recognized in jsx?

I have a multiple choice trivia game, I'm passing a boolean value of true or false to a custom data attribute .. 'correct' in the various answer components. 我有一个多项选择琐事游戏,我正在将true或false的布尔值传递给自定义数据属性..在各个答案组件中为“正确”。

When the answer is clicked on I'm running a check to see if the correct attribute has a value of true or false. 单击答案后,我正在检查是否正确的属性值为true或false。

Here is one of the answers 这是答案之一

 <Answer onClick={this.answerClick} onMouseEnter={this.hoverEnter} onMouseLeave={this.hoverLeave} correct={this.state.correctAnswer.a1}> {this.state.answerSet.a1} </Answer> 

And here is the answer component itself 这是答案组件本身

 const answer = (props) => ( <div className={classes.Answer} onMouseEnter={props.onMouseEnter} onMouseLeave={props.onMouseLeave} onClick={props.onClick} data-correct={props.correct}> {props.children} </div> ) 

So, on click I check to see if the data-correct attribute has a true or false value. 因此,在单击时,我将检查data-correct属性是否具有true或false值。

 answerClick(e) { console.log(e.target.dataset.correct) if(e.target.dataset.correct === true) { this.setState({ score: this.state.score +1, level: this.state.level +1}) console.log('you have answered correctly') } else { this.setState({ level: this.state.level +1 }) console.log('you have answered incorrectly') } } 

It only registers a false answer even when the correct one is selected. 即使选择了正确答案,它也只会记录错误答案。 I know that data-correct contains the value of true, because of my console log of the 我知道data-correct包含true值,因为我的控制台日志

console.log(e.target.dataset.correct)

Is there a jsx/react behavior taking place I'm not aware of? 我不知道是否发生了jsx / react行为? Why would this only default to the else when the value of data-correct is true? 为什么当data-correct的值为true时,这仅默认为else? Any help would be great. 任何帮助都会很棒。

DOM属性读取为字符串,而不是布尔值。

if(e.target.dataset.correct === 'true') {...}

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

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