我正在使用reactjs,并且在尝试显示json数据时(无论是来自文件还是服务器)似乎无法阻止此错误:
Uncaught TypeError: this.props.data.map is not a function
我看过:
React code throwing “TypeError: this.props.data.map is not a function”
React.js this.props.data.map() is not a function
这些都没有帮助我解决问题 . 我的页面加载后,我可以验证this.data.props没有未定义(并且有一个等效于json对象的值 - 可以使用 window.foo
调用),所以它似乎没有及时加载它被调用通过ConversationList . 如何确保 map
方法正在处理json数据而不是 undefined
变量?
var converter = new Showdown.converter();
var Conversation = React.createClass({
render: function() {
var rawMarkup = converter.makeHtml(this.props.children.toString());
return (
<div className="conversation panel panel-default">
<div className="panel-heading">
<h3 className="panel-title">
{this.props.id}
{this.props.last_message_snippet}
{this.props.other_user_id}
</h3>
</div>
<div className="panel-body">
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
</div>
</div>
);
}
});
var ConversationList = React.createClass({
render: function() {
window.foo = this.props.data;
var conversationNodes = this.props.data.map(function(conversation, index) {
return (
<Conversation id={conversation.id} key={index}>
last_message_snippet={conversation.last_message_snippet}
other_user_id={conversation.other_user_id}
</Conversation>
);
});
return (
<div className="conversationList">
{conversationNodes}
</div>
);
}
});
var ConversationBox = React.createClass({
loadConversationsFromServer: function() {
return $.ajax({
url: this.props.url,
dataType: 'json',
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentDidMount: function() {
this.loadConversationsFromServer();
setInterval(this.loadConversationsFromServer, this.props.pollInterval);
},
render: function() {
return (
<div className="conversationBox">
<h1>Conversations</h1>
<ConversationList data={this.state.data} />
</div>
);
}
});
$(document).on("page:change", function() {
var $content = $("#content");
if ($content.length > 0) {
React.render(
<ConversationBox url="/conversations.json" pollInterval={20000} />,
document.getElementById('content')
);
}
})
编辑:添加示例conversations.json
注意 - 调用 this.props.data.conversations
也会返回错误:
var conversationNodes = this.props.data.conversations.map...
回报
Uncaught TypeError: Cannot read property 'map' of undefined
这是conversations.json:
{“user_has_unread_messages”:false,“unread_messages_count”:0,“conversation”:[{“id”:18768,“last_message_snippet”:“Lorem ipsum”,“other_user_id”:10193}]}
6 回答
.map
函数仅在阵列上可用 .它看起来像
data
不是你期望它的格式(它是{}但你期待[]) .应该
检查要设置的“数据”类型,并确保它是一个数组 .
修改了一些建议的代码(propType validation和clearInterval):
更一般地说,您还可以将新数据转换为数组并使用concat之类的内容:
这种模式实际上用于Facebook的ToDo演示应用程序(请参阅"An Application"部分)https://facebook.github.io/react/ .
对我有用的是使用
data = Array.from(props.data);
将props.data转换为数组然后我可以使用data.map()
函数您不需要数组来执行此操作 .
之所以发生这种情况是因为组件在异步数据到达之前呈现,您应该在呈现之前进行控制 .
我用这种方式解决了这个问题:
this.props && this.props.partners.length > 0 ?
获取数据时尝试
componentDidMount()
生命周期