在React中使用map函数时的错误
Errors while Using map functions in React
我试图使用地图功能显示一些数据,但我不断得到警告和错误。它们是
警告:React性能度量代码中存在内部错误。没有期望componentDidMount定时器启动,而渲染定时器仍在进行另一个实例。
另一个是
Uncaught TypeError: Cannot read property 'map' of undefined
我发现this.props.courses。map返回null,但我仍然不能弄清楚为什么?
import React,{PropTypes} from 'react';
import {connect} from 'react-redux';
import * as courseActions from '../actions/courseActions';
class CoursesPage extends React.Component{
constructor(props,context){
super(props,context);
this.state={
course:[{id:1,title:'hello'}]
};
this.onTitleChange = this.onTitleChange.bind(this);
this.onClickSave = this.onClickSave.bind(this);
}
onTitleChange(event){
const course = this.state.course;
course.title = event.target.value;
this.setState({course:course});
}
onClickSave(){
this.props.dispatch(courseActions.createCourse(this.state.course));
}
courseRow(){
return this.props.courses.map((course)=>{
return(
<div key={course.id}>{course.title}</div>
);
});
}
render(){
return(
<div>
<h2>hello</h2>
<h3>{this.courseRow()}</h3>
<input type="text" onChange={this.onTitleChange} value={this.state.course.title}/>
<input type="submit" value="save" onClick={this.onClickSave}/>
</div>
);
}
}
CoursesPage.propTypes ={
dispatch: PropTypes.func.isRequired,
courses: PropTypes.array.isRequired
};
function mapStateToProps(state,ownProps){
return{
courses:state.courses
};
}
export default connect(mapStateToProps)(CoursesPage);
这是我的减速器
export default function courseReducer(state = [],action){
switch(action.type){
case 'CREATE_USER':
state.push(action.user);
return [...state,Object.assign({},action.user)];
default:
return state;
}
}
我rootReducer import {combineReducers} from 'redux';
import users from './userReducer';
import courses from './courseReducer';
const rootReducer = combineReducers({
users:users,
courses:courses
});
export default rootReducer;
商店import {createStore,applyMiddleware} from 'redux';
import rootReducer from '../reducers/rootReducer';
export default function configureStore(initialState){
return createStore(
rootReducer,
initialState
);
}
您的Redux存储的courses
可能在开始时是undefined
,因此this.props.courses
成为undefined
。试图访问undefined
的.map
会给你上面提到的错误。有几种方法可以解决这个问题:
方法1(推荐):将您的reducer中state.courses
的初始值设置为空数组[]
。参考Reducers的文档了解如何设置初始状态:http://redux.js.org/docs/basics/Reducers.html.
方法二:在mapStateToProps
中设置props.courses
的默认值
function mapStateToProps(state,ownProps){
return {
courses: state.courses || []
};
}
方法3:在渲染前检查this.props.courses
不是undefined
或null
:
courseRow() {
if (!this.props.courses) {
return null;
}
return this.props.courses.map((course)=>{
return(
<div key={course.id}>{course.title}</div>
);
});
}
你在哪里更新课程?我只能从你发布的代码中看到用户。
const initialState = {
courses : [],
}
export default function courseReducer(state =initialState,action){
switch(action.type){
//create a course or update it , else value is
//undefined and your map would throw error.
case 'CREATE_COURSES':
return Object.assign({},state,{
courses : action.user}
);
default:
return state;
}
}
同样在你的渲染方法中,你需要检查这个值是否被填充,然后只运行map。
<h3>{this.props.courses.length > 1 ? this.courseRow() : null}</h3>
在@Harkirat Saluja和@杨顺的帮助下修改代码后。我设法解决了初始化课程的问题,因此我将我的Reducer更改为如下
state = []成功了!
export default function courseReducer(state = [],action){
switch (action.type) {
case 'CREATE_COURSE':
return [...state,Object.assign({},action.course)];
default:
return state;
}
}
您的mapStateToProps
正在返回一个减速器,而不是您可以立即使用的实际props
。它应该看起来像这样:
function mapStateToProps(state,ownProps){
return{
...state.courses
};
}
- Node.js v6.2.0类扩展不是函数错误
- Javascript搜索函数错误
- Chrome Javascript日期构造函数错误
- 浏览器中出现javascript初始大写函数错误
- Createjs冲突问题localToLocal不是函数错误
- Java脚本函数错误
- 更新带有Javascript函数错误的变量
- 如何将对象插入对象列表的开头,当它有 unshift 不是函数错误
- 在将 Pickadate 与 Meteor 一起使用时,未定义不是函数错误
- 运行咕噜咕噜咕噜抛出 _.object 不是函数错误
- 了解函数(错误、数据)回调
- javascript中的typeof()函数错误
- 我一直在获取angularjs服务不是函数错误(LoginService.login不是函数)
- HTML5拖拽&drop正在抛出undefined不是函数错误
- Google Maps API a.lat不是函数错误
- getItemAtIndex不是函数错误
- Javascript停止函数错误
- 对话框函数错误无效函数
- Javascript 自定义函数错误
- 因果报应单元测试'未定义'不是函数错误