I'm building a small react js application and I get this error:
Uncaught Error: Invariant Violation: exports.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.
These are my source files: App.jsx
var React = require('react');
var Chats = require('./chats');
var messages = {
chatMessages: [{
name: ' John Doe',
message: ' Hey Lisa, how are you doing?'
},{
name: 'Lisa Johnson',
message: 'Pretty good, how are you?'
}]
}
var App = React.createElement(Chats, messages)
React.render(App, document.querySelector('.container'));
Chats.jsx
var React = require ('react');
var Chat = require ('./chat');
var Chats = React.createClass({
render:function(){
var chats = this.props.chatMessages.map(function(chatProps){
return <Chat {...chatProps} />
});
return (
<div>
{chats}
</div>
)
}
});
module.exports = Chats;
And Chat.jsx
var React = require ('react');
var Chat = React.createClass ({
render:function (){
return
<div>
<p>{this.props.name}</p>
<span> {this.props.message} </span>
</div>
}
});
module.exports = Chat;
I think the problem is in my map function but I really want someone to tell me where I made the mistake.
Chat
elements from theChats
render function; they need a parent element. Try<div>{chats}</div>
instead (or the JSX-less equivalent). – Josh David Millerkey
prop for each item in an array that points to an unique id of some kind so React knows which DOM nodes are for the same data when it renders again. – Josh David Millerreturn (<div>...</div);
And can you identify which line is causing the error? – Josh David Miller