【发布时间】:2016-10-23 08:37:37
【问题描述】:
为什么我在实时服务器上收到此错误:
未捕获的类型错误:this.state.navitems.map 不是函数
但我的本地主机上从未出现此错误。
reactjs:
import React from 'react'; import $ from 'jquery';
import NavItem from './nav-item';
class Footer extends React.Component {
constructor(props) {
super(props);
this.state = {
navitems: [],
};
}
// Then fetch the data using $.get():
componentDidMount() {
this.serverRequest = $.get(this.props.source, function (result) {
this.setState({
navitems: result
});
}.bind(this));
}
componentWillUnmount() {
this.serverRequest.abort();
}
render() {
var loop = this.state.navitems.map(function(item, index){
return <NavItem key={index} item={item}></NavItem>;
});
return (
<div>
<div className="nav">
<ul>{ loop }</ul>
</div>
</div>
)
} }
export { Footer as default }
有什么办法可以解决这个问题吗?
编辑:
componentDidMount() {
console.log('props.source', this.props.source);
this.serverRequest = $.get(this.props.source, function (result) {
console.log('returned result', result);
this.setState({
navitems: result
});
}.bind(this));
}
结果:
props.source ./data/nav.json
returned result [{
"navId": "1",
"title": "Home",
"code": "home",
"href": null,
"style": null,
"sort": "1",
"url": "#",
"parentId": null,
"totalChildren": "0",
"createdOn": null,
"updatedOn": null
}, {
"navId": "2",
"title": "About",
"code": "about",
"href": null,
"style": null,
"sort": "2",
"url": "#",
"parentId": null,
"totalChildren": "0",
"createdOn": null,
"updatedOn": null
}, {
"navId": "3",
"title": "Contact",
"code": "contact",
"href": null,
"style": null,
"sort": "3",
"url": "contact",
"parentId": null,
"totalChildren": "0",
"createdOn": null,
"updatedOn": null
}]
【问题讨论】:
标签: jquery reactjs ecmascript-6