I'm beginning Reactjs and now I have an issue with my app which doesn't work half of the time but works sometime. I guess the problem is related to ComponentDidMount but I can't fix it. My app consists in testing your ability to translate some words which are stored in my state, the functions are very simple so I don't understand why the app crashes sometime. The erro says " Cannot read property 'francais' of undefined "
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
应用程序不会崩溃
Can you try something like this without did mount
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.