gpt4 book ai didi

javascript - React JS - 未捕获的 TypeError : this. props.data.map 不是函数

转载 作者:IT老高 更新时间:2023-10-28 12:43:09 26 4
gpt4 key购买 nike

我正在使用 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,"conversations":[{"id":18768,"last_message_snippet":"Lorem ipsum","other_user_id":10193}]}

最佳答案

.map 函数仅适用于数组。
data 似乎不是您期望的格式(它是 {},但您期望的是 [])。

this.setState({data: data});

应该是

this.setState({data: data.conversations});

检查“数据”设置为什么类型,并确保它是一个数组。

带有一些建议的修改代码(propType 验证和 clearInterval):

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({
// Make sure this.props.data is an array
propTypes: {
data: React.PropTypes.array.isRequired
},
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.conversations});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},

/* Taken from
https://facebook.github.io/react/docs/reusable-components.html#mixins
clears all intervals after component is unmounted
*/
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.map(clearInterval);
},

componentDidMount: function() {
this.loadConversationsFromServer();
this.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')
);
}
})

关于javascript - React JS - 未捕获的 TypeError : this. props.data.map 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30142361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com