【发布时间】:2019-01-03 06:31:35
【问题描述】:
我遇到了问题:this.setState({score: counter});由于某种原因,它没有更新。为什么?即使我这样做了 this.setState({score: 5}) 或 score:"a string" 它仍然不起作用。我错过了什么吗?无论我做什么,状态都不会改变。我必须添加额外的描述,因为网站不允许我发布我的问题。
displayResultsHandler = () => {
var counter = 0;
for(let i = 0; i < this.state.correctAnswers.length; i++) {
if(this.state.correctAnswers[i] === this.state.userAnswers[i]) {
counter = counter+1;
}
}
this.setState({
score: counter
});
console.log("counter: " + counter);
console.log("score: " + this.state.score); // For some reason, this is always zero
}
我在下面添加了我的完整代码。也许会有所帮助。
import React, { Component } from 'react';
import './App.css';
import Title from './Title/Title';
import Question from './Question/Question';
import Aux from './hoc/Aux';
import SubmitButton from './SubmitButton/SubmitButton';
class App extends Component {
state = {
questionArray: [
"What is 9+10",
"How many goals did Ronaldo score against Spain in the World Cup 2018",
"Who Stole Ronaldo's (CR7) greates ever goal?",
"Which one of these players ruined the NBA",
"Who is currently number 1 in the internet L rankings?"
],
answerChoicesArray: [
["1", "19", "21", "90", "-1"],
["1", "3", "5", "0", "-1"],
["Pepe", "Messi", "Casillas", "Benzema", "Nani"],
["Allen Iverson", "Kevin Durant", "Steph Curry", "Lebron James", "Russel Westbrook"],
["Drake", "Pusha T", "Russel Westbrook", "Lil Xan", "Russ"]
],
correctAnswers: ["21", "3", "Nani", "Kevin Durant", "Russ"],
userAnswers: ["", "", "", "", ""],
score: 0
}
updateUserAnswersHandler = (oldArray, index, value) => {
const newArray = oldArray;
newArray[index] = value;
this.setState({
userAnswers: newArray
});
}
displayResultsHandler = () => {
var counter = 0;
for(let i = 0; i < this.state.correctAnswers.length; i++) {
if(this.state.correctAnswers[i] === this.state.userAnswers[i]) {
counter = counter+1;
}
}
this.setState({
score: counter
});
console.log("counter: " + counter);
console.log("score: " + this.state.score); // For some reason, this is always zero
if(this.state.score < 5) {
alert("You're dumb asf. Please leave");
} else {
alert("Welcome to NASA");
}
}
render() {
// var userAnswers;
// console.log(userAnswers); How do I pass this as a prop? this.userAnswers? nope. Therefore it has to be a state
console.log("User Answers are: " + this.state.userAnswers);
return (
<div className="App">
<div className="container">
<Title/>
<h2>Only the most genius of individuals will pass</h2>
<hr/>
<Question
correctAnswers={this.state.correctAnswers}
updateUserAnswersHandler={this.updateUserAnswersHandler}
userAnswers={this.state.userAnswers}
questionArray={this.state.questionArray}
answerChoicesArray={this.state.answerChoicesArray} />
<SubmitButton
clicked = {this.displayResultsHandler} />
</div>
</div>
);
}
}
export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
【问题讨论】:
-
你在类中添加了构造函数吗?
-
没有。我以为我们不需要最新版本的构造函数?因为我能够在没有构造函数的情况下更改我的 userAnswers 状态
标签: javascript reactjs state setstate