【发布时间】:2021-01-28 09:39:55
【问题描述】:
我正在编写一个反应应用程序来从后端数据库中获取词汇表,然后将其显示给用户,以及有关每个特定单词的一些信息。我的问题是,由于数组的索引未定义,我无法在 render() 中显示单词,但是根据 chrome devtools,数组已完全定义。
这是我的代码:
import React from 'react';
import "./Vocab.css"
class Vocab extends React.Component {
//HEADER HEADER HEADER
state = {
vocabulary: null,
var2: null
};
componentDidMount(){
//Initializing Routine Here
this.getVocab()
}
getVocab = () => {
var newVocab = [];
if(this.state.vocabulary == null){
newVocab = newVocab;
}
else{
newVocab = [this.state.vocabulary];
}
this.props.HandleFetch("GET", this.props.backLink + "vocab")
.then(r => JSON.parse(r))
.then(data => newVocab.push(data[0]))
.then(this.setState({vocabulary:newVocab}))
}
//In render() variable display and the if/else statements are used exclusively for testing purposes
render() {
var display = "X";
if(this.state.vocabulary == null){
display = "Y";
}
else{
console.log(this.state.vocabulary); //Outputs the array
console.log(this.state.vocabulary[0]) //Outputs "undefined"
display = "Z";
}
return (
<>
<h1>Test</h1>
{display}
</>
);
}
}
export default Vocab;
console.log(this.state.vocabulary)的输出:
0:
Definition: "TestDef"
IPA: "TestIPA"
IsCard: true
Language: "TestLang"
LastStudyDate: "2021-01-27"
Mnem: ""
PoS: "TestPO"
Source: "TestDict"
SourceLearned: "TestSource"
Word: "Test"
this.state.vocabulary[0]的输出:
undefined
此外,根据 chrome devtools 并通过在控制台中键入 $r.state.vocabulary[0] 我在数组的索引处获得字典,这是我需要的:
{Word: "Test", Mnem: "", LastStudyDate: "2021-01-27", SourceLearned: "TestSource", IsCard: true, …}
在控制台中,数组是完全可导航的,但网页本身不会呈现它。知道我可能做错了什么吗?
更新 我能够添加一个按钮,onClick 转到打印出词汇和词汇[0] 的功能。这确实有效,但 render() 函数和 componentDidUpdate() 函数仍然无法访问词汇表[0]。我在 react 文档中没有看到任何关于 setState() 完成或在 componentDidUpdate() 之后发生的任何函数的内容。
此外,在早期的一些测试中,我尝试 console.log newVocab[0] 在将其分配给 .then 字符串中的状态之前。这有效,但仅在我所有其他控制台调用之后才在控制台中输出,这表明它发生在组件重新渲染和更新周期之后,即使我在调用 setState() 之前调用它。
我目前的测试代码:
import "./Vocab.css"
class Vocab extends React.Component {
//HEADER HEADER HEADER
state = {
vocabulary: null,
var2: null
};
componentDidMount(){
//Initializing Routine Here
this.getVocab()
}
componentDidUpdate(){
console.log("Vocab update");
console.log(this.state.vocabulary);
console.log("Vocab[0] update");
console.log(this.state.vocabulary[0]);
///Outputs the vocabulary array, and then 'undefined'
}
getVocab = () => {
var newVocab = [];
if(this.state.vocabulary == null){
newVocab = newVocab;
}
else{
newVocab = this.state.vocabulary;
}
this.props.HandleFetch("GET", this.props.backLink + "vocab")
.then(r => JSON.parse(r))
.then(data => newVocab.push(data[0]))
.then(this.setState({vocabulary:newVocab}))
}
logVocab = () => {
console.log("Vocab");
console.log(this.state.vocabulary);
console.log("Vocab[0]");
console.log(this.state.vocabulary[0]);
///Outputs the vocabulary array, and then the dictionary at index[0].
}
render() {
var display = "X";
if(this.state.vocabulary == null){
display = "Y";
console.log("Y");
}
else{
console.log("Vocab Z");
console.log(this.state.vocabulary);
console.log("Vocab[0] Z");
console.log(this.state.vocabulary[0]);
display = "Z";
///Outputs the vocabulary array, and then 'undefined'
}
return (
<>
<h1>Test</h1>
{this.state.vocabulary ? this.state.vocabulary[0] : ""}
<button onClick = {this.logVocab}> CLICK </button>
</>
);
}
}
export default Vocab;
【问题讨论】:
-
你能从
this.props.HandleFetch("GET", this.props.backLink + "vocab") .then(r => JSON.parse(r))登录你的r吗 -
@antoineso 确定!这是
[{"Word": "Test", "Mnem": "", "LastStudyDate": "2021-01-27", "SourceLearned": "TestSource", "IsCard": true, "IPA": "TestIPA", "PoS": "TestPO", "Definition": "TestDef", "Source": "TestDict", "Language": "TestLang"}]
标签: javascript arrays reactjs