Rendering JavaScript Object in React

我的梦境 提交于 2019-12-06 12:29:08

Change your Message render method to:

render() {
  return <div>{this.props.data}</div>
}

You are passing the prop to the message component by name data and rendering it using this.props.message which is wrong.

Use this.props.data.

export default class Message extends React.Component {

constructor(props){ 
    super(props); 
} 
render(){ 
    return ( 
       <div> {this.props.data} </div> 
    ) 
 }

}

Regarding the error `Objects are not valid as a React child (found: object with keys {function, title, key}). I changed my prop from an object to an array. Finally I changed my message component to this.props.data since no message prop was passed as the other posters have noted.

The array fix:

class ProjectList extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      messages: []
    };

Thank you so much for the help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!