我正在尝试让 API 提取一个词并将其设置为状态。然后一个函数将读取该状态并完成其设计目的。然而,我最初编码它的方式称它为乱序。第三个代码片段允许代码成功运行,但我不确定为什么。有人能解释一下有什么区别或者为什么原来的方法不起作用吗?
下面是第二个函数后面的 API 函数。
wordNikApi = () => {
fetch("http://api.wordnik.com:80/v4/words.json/randomWords?hasDictionaryDef=true&minCorpusCount=0&minLength=5&maxLength=15&limit=1&api_key=/* Removed */")
.then( res => res.json() )
.then( ( result ) => {
this.setState({
apiWord: result[0].word,
});
console.log("wordNikApi: ", this.state.apiWord);
})
.catch( ( error ) => {
console.log("API ERROR: ", error);
})
};
resetGame = () => {
console.log("resetGame");
this.wordNikApi();
this.setState({
word: [],
count: 0,
allAttempts: [],
letterIndex: [],
numberOfBadAttempts: 0,
remainingAttempts: 6,
repeat: false,
pageLock: false,
invalidKey: false,
}, () => {
console.log("resetGame: function 1");
console.log(this.state.apiWord);
let fullWord = "word";
let wordArray = fullWord.split("");
let wordLength = wordArray.length;
// Send wordObj to state with value and index
let wordObj = wordArray.map((value, index) => {
return {
found: false,
val: value,
id: index,
}
})
this.setState({
word: wordObj,
wordLength: wordLength,
remainingAttempts: 6,
});
});
};
DIEA
蛊毒传说
GCT1015
相关分类