[英]React App working only working half of the time
我正在开始使用 Reactjs,但现在我的应用程序出现了一个问题,该问题在一半时间无法运行,但有时可以运行。 我猜这个问题与 ComponentDidMount 有关,但我无法修复它。 我的应用程序包括测试您翻译存储在我的状态中的一些单词的能力,功能非常简单,所以我不明白为什么应用程序有时会崩溃。 错误说“无法读取未定义的属性‘法郎’ ”
import React from 'react';
import './App.css';
class App extends React.Component {
constructor() {
super();
this.state = {
words: [
{
francais: 'bonjour',
anglais: 'hello',
},
{
francais: 'manger',
anglais: 'eat',
},
{
francais: 'avoir',
anglais: 'have',
},
{
francais: 'faire',
anglais: 'do',
},
{
francais: 'jouer',
anglais: 'play',
}
],
key: -1.4,
currentWord: '',
currentAnswer: '',
correctAnswer: false,
giveUp: false
}
}
generateWord = () => {
let index = Math.floor(Math.random() * (this.state.words.length + 1))
if(index === this.state.key) {
this.generateWord()
}
this.setState({currentWord: this.state.words[index]})
this.setState({key: index})
}
validate = (e) => {
e.preventDefault()
const answer = e.target.value
this.setState({ currentAnswer: answer })
if (this.state.currentAnswer !== this.state.currentWord.anglais) {
this.setState({correctAnswer : false})
}
else {
this.setState({correctAnswer : true})
}
}
showCorrection = (e) => {
e.preventDefault()
this.setState({giveUp: true})
}
nextWord = (e) => {
e.preventDefault()
this.setState({currentAnswer: ''})
this.setState({ giveUp: false })
this.generateWord()
}
componentDidMount() {
this.generateWord()
}
render() {
return (
<div className="App">
<h2 style={{
color: "midnightblue",
fontSize: "50px"
}}>{this.state.currentWord.francais}</h2>
<form action="">
<input onChange={this.validate} value={this.state.currentAnswer} type="text" placeholder="Entrez la traduction anglaise"/>
<button className="validation" onClick={this.showCorrection}>Give up</button>
<button className="validation" onClick={this.nextWord}>Next</button>
</form>
{this.state.correctAnswer ? <p>Correct !</p> : this.state.giveUp ? <p>La bonne réponse était: {this.state.currentWord.anglais}</p>: ''}
</div>
)
}
}
export default App;
尝试添加this.state.currentWord?.francais
而不是this.state.currentWord.francais
应用程序不会崩溃
你能在没有安装的情况下尝试这样的事情吗
import React from 'react';
import './App.css';
class App extends React.Component {
constructor() {
super();
this.state = {
words: [
{
francais: 'bonjour',
anglais: 'hello',
},
{
francais: 'manger',
anglais: 'eat',
},
{
francais: 'avoir',
anglais: 'have',
},
{
francais: 'faire',
anglais: 'do',
},
{
francais: 'jouer',
anglais: 'play',
}
],
key: -1.4,
currentWord: '',
currentAnswer: '',
correctAnswer: false,
giveUp: false
}
}
generateWord = () => {
let index = Math.floor(Math.random() * (this.state.words.length + 1))
if(index === this.state.key) {
this.generateWord()
}
this.setState({currentWord: this.state.words[index]})
this.setState({key: index})
}
validate = (e) => {
this.generateWord()
const answer = e.target.value
this.setState({ currentAnswer: answer })
if (answer !== this.state.currentWord.anglais) {
this.setState({correctAnswer : false})
}
else {
this.setState({correctAnswer : true})
}
}
showCorrection = (e) => {
e.preventDefault()
this.setState({giveUp: true})
}
nextWord = (e) => {
e.preventDefault()
this.setState({currentAnswer: ''})
this.setState({ giveUp: false })
this.generateWord()
}
render() {
return (
<div className="App">
<h2 style={{
color: "midnightblue",
fontSize: "50px"
}}>{this.state.currentWord.francais}</h2>
<form action="">
<input onChange={this.validate} value={this.state.currentAnswer} type="text" placeholder="Entrez la traduction anglaise"/>
<button className="validation" onClick={this.showCorrection}>Give up</button>
<button className="validation" onClick={this.nextWord}>Next</button>
</form>
{this.state.correctAnswer ? <p>Correct !</p> : this.state.giveUp ? <p>La bonne réponse était: {this.state.currentWord.anglais}</p>: ''}
</div>
)
}
}
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.