json - 为什么我的组件在 ReactJS 中返回一个空对象?
问题描述
我遇到了一个问题,将数据从我的 JSON 文件传递到我尝试创建的测验组件。到目前为止,我有幸通过一个简单的 JSON 文件传递数据,该文件存储在我的 Src 文件夹中。但是,当我尝试访问位于我的公用文件夹中的更大文件时,没有返回任何内容。它不是返回一个填充了“当前问题”和“当前选项”的对象,而是返回一个空白对象。
如果应用程序显示某种错误会更有帮助,但因为我不知道是什么原因造成的,所以我现在不知所措。
这是用于生成问题的主文件,我利用这个组件从我的 JSON 文件中传递属性。
class Play extends React.Component {
constructor(props){
super(props);
this.state = {
questions: this.props.questions,
currentQuestion: {},
nextQuestion: {},
previousQuestion: {},
answer: "",
currentQuestionIndex: 0,
}
}
componentDidMount(){
const {questions, currentQuestion, nextQuestion, previousQuestion} = this.state;
this.displayQuestions(questions,currentQuestion, nextQuestion,previousQuestion)
}
displayQuestions = (questions = this.state.questions, currentQuestion, nextQuestion, previousQuestion) => {
let { currentQuestionIndex } = this.state;
if(!IsEmpty(this.state.questions)){
questions = this.state.questions;
currentQuestion = questions[currentQuestionIndex];
nextQuestion = questions[currentQuestionIndex + 1];
previousQuestion = questions[currentQuestionIndex - 1];
const answer = currentQuestion.answer;
this.setState({
currentQuestion,
nextQuestion,
previousQuestion,
answer
});
}
}
render(){
const {currentQuestion} = this.state;
return(
<React.Fragment>
<Container>
<H5> {currentQuestion.question}</H5>
<OptionsContainer>
<ul>
{!!currentQuestion.options && currentQuestion.options.map((option) =>
<p className = "option"> {option.option}</p>
)}
</ul>
</OptionsContainer>
</Container>
</React.Fragment>
)
}
}
这就是我使用 Play 组件从下面的 JSON 文件中动态传递数据的方式。
<Play questions = {this.props.questions}/>
这是从动态呈现 url 并将每组数据放在单独的组件中的组件的片段。
<Route path = {`${url}/:topicId`} render = {
({ match }) =>
<TopicHeaderCard {...coreTopics.find(topic => topic.id === match.params.topicId)}/>}
/>
我的 JSON 文件片段,用于获取测验信息(目前非常基本)
"questions": [
{
"question": "How tall is your fridge?",
"options": [
{
"option": "6 feet"
},
{
"option": "12 feet"
},
{
"option": "3 feet"
},
{
"option": "1 foot"
}
]
}
提前致谢,感谢所有帮助!
解决方案
我认为问题在于道具在通过之前需要花费一些时间来加载。因此,您应该尝试使用 componentDidupdate 而不是 componentDidMount,以便在 props 更新数据时可以使用它。
我不确定下面的代码是否可行,只需尝试一下并适应您的偏好。
class Play extends React.Component {
constructor(props){
super(props);
this.state = {
questions: [],
currentQuestion: {},
nextQuestion: {},
previousQuestion: {},
answer: "",
currentQuestionIndex: 0,
initComplete: false
}
}
componentDidUpdate(){
const {questions} = this.props;
if(!IsEmpty(questions) && !initComplete){
currentQuestion = questions[currentQuestionIndex];
nextQuestion = questions[currentQuestionIndex + 1];
previousQuestion = questions[currentQuestionIndex - 1];
const answer = currentQuestion.answer;
this.setState({
questions,
currentQuestion,
nextQuestion,
previousQuestion,
answer,
initComplete = true
});
}
render(){
const {currentQuestion} = this.state;
return(
<React.Fragment>
<Container>
<H5> {currentQuestion.question}</H5>
<OptionsContainer>
<ul>
{!!currentQuestion.options && currentQuestion.options.map((option) =>
<p className = "option"> {option.option}</p>
)}
</ul>
</OptionsContainer>
</Container>
</React.Fragment>
)
}
}
推荐阅读
- android - 解析平台 - 检查包含在对象数组中的“键”的“值”
- winapi - 我无法循环浏览窗口中的所有控件,包括选项卡
- python - 自编码器网络中的数组形状
- sql-server - SQL-选择一个值大于第一个值日的行
- c# - 给基于ITypeEditor的WPF自定义控件添加参数?
- java - 使用Junit测试Runnable方法,for循环内部的逻辑意外只执行了一次
- javascript - ReactJS 关于状态数组及其异步的问题
- angular - 如何在 Angular 的 Typescript 中使用数组
- git - 如何在 IntelliJ IDEA 中查看本地与远程分支状态
- php - WordPress 使用 IN() 为多种帖子类型准备 SQL 查询字符串,并从 SQL 中删除反斜杠 (\)