亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React App 只工作了一半的時間

React App 只工作了一半的時間

米脂 2022-06-09 10:36:30
我正在開始使用 Reactjs,現在我的應用程序出現了問題,它有一半的時間不能工作,但有時可以工作。我猜這個問題與 ComponentDidMount 有關,但我無法修復它。我的應用程序包括測試您翻譯存儲在我的狀態中的一些單詞的能力,功能非常簡單,所以我不明白為什么應用程序有時會崩潰。錯誤說“無法讀取未定義的屬性'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) => { 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() {
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

你能在沒有安裝的情況下嘗試這樣的事情嗎


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;


查看完整回答
反對 回復 2022-06-09
?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

嘗試添加這個this.state.currentWord?.francais而不是this.state.currentWord.francais應用程序不會崩潰



查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號