Sto lavorando con reactjs e non riesco evitare questo errore quando si cerca di visualizzare i dati JSON (sia da file o server):Reagire JS - TypeError Uncaught: this.props.data.map non è una funzione
Uncaught TypeError: this.props.data.map is not a function
ho guardato:
React code throwing “TypeError: this.props.data.map is not a function”
React.js this.props.data.map() is not a function
Nessuno di questi mi ha aiutato a risolvere il problema. Dopo il caricamento della pagina, posso verificare che questo.data.props non sia indefinito (e abbia un valore equivalente all'oggetto json - può chiamare con window.foo
), quindi sembra che non si carichi in tempo quando viene chiamato per ConversationList. Come faccio a essere sicuro che il metodo map
funzioni sui dati JSON e non su una variabile 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')
);
}
})
EDIT: aggiunta di campione conversations.json
Nota - chiamando this.props.data.conversations
restituisce anche un errore:
var conversationNodes = this.props.data.conversations.map...
rendimenti
Uncaught TypeError: Cannot read property 'map' of undefined
Ecco conversations.json:
{ "user_has_unread_messages": false, "unread_messages_count": 0, "conversazioni": [{ "id": 18768, "last_message_snippet": "Lorem ipsum", "other_user_id": 10193}]}
può voi pubblica un esempio di conversations.json? – user120242
Aggiunto. Molte grazie! – keypulsations
Ho aggiornato la risposta. Inoltre, alcuni consigli: aggiungi propTypes: {data: React.PropTypes.array} a ConversationList per verificare il tipo di dati e aggiungi un componenteWillUnmount: fn() che "clearInterval" l'intervallo in ConversationBox. – user120242