I'm new to react and have created a simple app from a tutorial. In the tutorial, they have used stateless components whereas I changed to stateful components. But I'm getting this error
TypeError: Cannot read property 'map' of undefined
I don't know where my code is getting wrong. It seems to be all right for me.
message-list.js
import React, { Component } from 'react';
import MessageView from './message-view';
class MessageList extends Component {
state = {
messages: [
{
from: 'John',
message: 'The event will start next week',
status: 'unread'
},
{
from: 'Martha',
message: 'I will be traveling soon',
status: 'read'
},
{
from: 'Jacob',
message: 'Talk later. Have a great day!',
status: 'read'
}
]
};
render() {
const { messageViews } = this.state.messages;
console.log(messageViews);
return (
<div>
<h1>List of Messages</h1>
{messageViews.map(msgList => (
<MessageView message={msgList.messages} />
))}
</div>
);
}
}
export default MessageList;
message-view.js
import React, { Component } from 'react';
class MessageView extends Component {
render() {
const list = this.props.message;
console.log(list);
return (
<div className="container">
<div className="from">
<span className="label">From : </span>
<span className="value">{list.name} </span>
</div>
<div className="status">
<span className="label">Status : </span>
<span className="value">{list.status}</span>
</div>
<div className="message">
<span className="label">Message : </span>
<span className="value">{list.content} </span>
</div>
</div>
);
}
}
export default MessageView;
I have tried searching why map function is not working but of no-avail. I'm very much new to react. Can someone please guide me on this.