自己写一个简单的React组件的时候,因为
Objects are not valid as a React child (found: Sat Feb 23 2019 16:18:01 GMT+0800 (China Standard Time)). If you meant to render a collection of children, use an array instead.
这个错误卡了好久,心累
最后才发现是因为在render函数中渲染了对象导致的
下面是错误的代码
class Clock extends Component {
constructor(props) {
super(props)
this.state = {
date: new Date()
}
}
componentWillMount() {
setInterval(()=>{
console.log(this.state.date.toLocaleString())
this.setState({
date: new Date()
})
}, 1000)
}
render() {
return (
<div>
<h1>Hello, world!</h1>
// 出错的地方
<h2>It is {this.state.date}</h2>
</div>
);
}
}