javascript - TypeError:无法读取未定义 Reactjs 的属性“地图”?
问题描述
TypeError:无法读取未定义 Reactjs 的属性“地图”?
我正在使用小测验模块,但显示以下错误,请帮助我解决以下错误 TypeError: Cannot read property 'map' of undefined 我不知道如何解决我是新手,请帮助我获取此代码
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Questionlist from './quiz/Questionlist.jsx';
import * as serviceWorker from './serviceWorker';
class Quiz extends React.Component {
constructor(props){
super(props);
this.state= {
questions : [
{
id: 1,
text: 'What is your name?',
choices:[
{
id: 'a',
text: 'Michael'
},
{
id: 'b',
text: 'Brand'
},
{
id: 'c',
text: 'Steven'
},
],
correct: 'b'
},
{
id: 2,
text: 'What is your mother name?',
choices:[
{
id: 'a',
text: 'Sara'
},
{
id: 'b',
text: 'Denny'
},
{
id: 'c',
text: 'senny'
},
],
correct: 'c'
},
{
id: 3,
text: 'What is your father name?',
choices:[
{
id: 'a',
text: 'Bobby'
},
{
id: 'b',
text: 'Harry'
},
{
id: 'c',
text: 'Waye'
},
],
correct: 'c'
},
{
id: 4,
text: 'What is your friend name?',
choices:[
{
id: 'a',
text: 'John'
},
{
id: 'b',
text: 'Paul'
},
{
id: 'c',
text: 'Jose'
},
],
correct: 'a'
},
],
score: 0,
current: 1
}
}
render() {
return <h2>I am a Car!</h2>;
}
}
class Garage extends React.Component {
render() {
return (
<div>
<Questionlist />
</div>
);
}
}
ReactDOM.render(<Garage />, document.getElementById('root'));
serviceWorker.unregister();
问题列表.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import Question from './Question.jsx';
class Questionlist extends React.Component {
render() {
return(
<div className="question">
{
this.props.questions.map(questions => {
return <Question questions={questions} key={questions.id} {...this.props} />
})
}
</div>
)
}
}
export default Questionlist
问题.jsx
import React from 'react';
import ReactDOM from 'react-dom';
class Question extends React.Component {
render() {
const {question} = this.props;
return(
<div className="well">
<h3>{question.text}</h3>
<hr />
<ul className="list-group">
{
this.props.question.choices.map(choice =>{
return(
<li className="list-group-item">
{choice.id} <input type="radio" onChange={this.onChange.bind(this)} name={question.id} value={choice.id} /> {choice.text}
</li>
)
})
}
</ul>
</div>
)
}
}
export default Question
解决方案
将您的数组传递给Questionlist组件。
如果没有用,请尝试删除您的Garage组件,因为 Quiz 组件中有数组(问题)。将问题从 Quiz 组件传递到Questionlist组件。
代替
render() {
return <h2>I am a Car!</h2>;
}
像这样通过
render() {
return <Questionlist questions={this.state.questions} />
}
最后将根组件从Garage更改为Quiz
ReactDOM.render(<Quiz />, document.getElementById('root'));
推荐阅读
- python - 如果用户在 django 中注册后第一次登录,如何将用户重定向到不同的页面?
- python - 有没有一种快速的方法可以有条件地将循环添加到 python 中的 for 循环?
- vue.js - 满足 v-show 条件后调用 API
- c++ - STL make_heap、pop_heap 的自定义比较功能,如何使其在图形搜索例程中工作?
- node.js - 节点未安装并显示错误
- ubuntu - 如何从 PDF 文件中删除流/对象?
- python - AttributeError:“系列”对象没有属性“合并”
- python - 运行 docker 时无法将“app.server”解析为属性名称或函数调用
- html - 我无法使用我使用 HTML 和 CSS 制作的 github 页面发布我的网站
- python - 我无法使用“枕头”的“裁剪”来剪切图像