React抛出错误:不能读取property 'map'的定义
React throwing error: Cannot read property 'map' of undefined
我正在学习React。我正在阅读书中的一章,其中他们从componentDidMount
下的API加载数据,并将组件的状态更新为
getInitialState: function () {
return {changeSets: []};
},
mapOpenLibraryDataToChangeSet : function (data) {
return data.map(function (change, index) {
return {
"when":jQuery.timeago(change.timestamp),
"who": change.author.key,
"description": change.comment
}
});
},
componentDidMount: function () {
$.ajax({
url: 'http://openlibrary.org/recentchanges.json?limit=10',
context: this,
dataType: 'json',
type: 'GET'
}).done(function (data) {
// console.log(data);
var changeSets = this.mapOpenLibraryDataToChangeSet(data);
console.log("changeSets:" + JSON.stringify(changeSets));
this.setState({changeSets: changeSets});
});
},
当我运行这个时,我在控制台看到错误
"TypeError: Cannot read property 'map' of undefined
at t.render (mikobuf.js:55:41)
at _renderValidatedComponentWithoutOwnerOrContext (https://npmcdn.com/react@15.3.1/dist/react.min.js:13:17508)
at _renderValidatedComponent (https://npmcdn.com/react@15.3.1/dist/react.min.js:13:17644)
at performInitialMount (https://npmcdn.com/react@15.3.1/dist/react.min.js:13:13421)
at mountComponent (https://npmcdn.com/react@15.3.1/dist/react.min.js:13:12467)
at Object.mountComponent (https://npmcdn.com/react@15.3.1/dist/react.min.js:15:2892)
at h.mountChildren (https://npmcdn.com/react@15.3.1/dist/react.min.js:14:26368)
at h._createInitialChildren (https://npmcdn.com/react@15.3.1/dist/react.min.js:13:26619)
at h.mountComponent (https://npmcdn.com/react@15.3.1/dist/react.min.js:13:24771)
at Object.mountComponent (https://npmcdn.com/react@15.3.1/dist/react.min.js:15:2892)"
运行链接为http://jsbin.com/mikobuf/edit?js,console,output
我做错了什么?
当我在渲染应用程序时添加changeSets={data}
时,我在控制台看到数据
ReactDOM.render(<App headings = {headings} changeSets={data}/>, document.getElementById("container"))
但是我想从API
中提取数据。因此,只要我在渲染时删除changeSets={data}
,它就会失败
ReactDOM.render(<App headings = {headings}/>, document.getElementById("container"))
您正在尝试使用道具changeSets
时,它实际上是App
的状态的一部分。
:
<RecentChangesTable.Rows changeSets={this.props.changeSets} />
<RecentChangesTable.Rows changeSets={this.state.changeSets} />
http://jsbin.com/tuqeciyere/1/edit?js、控制台、输出
相关文章:
- 变得断断续续'谷歌没有定义'以及使用map-apiv3的其他错误
- Angular Google Map的自定义样式
- map.getBounds() 奇怪的行为(未捕获的类型错误:无法读取未定义的属性“getNorthEast”)
- 什么是谷歌应用脚本(自定义电子表格函数)示例中的“input.map”
- 为什么 Array.map 似乎不适用于未定义值的数组
- 无法在我的自定义类(Javascript)中继承google.maps.Map V3
- 将自定义地图标记图标添加到Google map API
- 'document.getElementsByClassName' 的结果没有定义像 'map' 这样的数组方法,即使它
- jQuery.map()返回而未定义
- 如何使用jquery、json和自定义数据属性将map方法嵌套在map方法中
- 回调函数的定义以及它如何适用于array.prototype.forEach/map/filter方法
- .map()img src属性未定义
- 饼状图显示未捕获类型错误:不能调用方法'map'的定义
- Rails和React: TypeError: Cannot call method 'map'的定义
- 在自定义标记Google Map V3中放置一个值
- 如何将原型上定义的方法作为回调传递给Array.map
- 参考错误:谷歌不是使用 ng-map GeoCoder 服务定义的
- 它不是在ES6中使用map()定义的
- 如何用ForEach定义Map
- 使用此方法访问变量将在map中返回未定义