javascript - 如何在 React 组件中使用对象数据
问题描述
我试图制作一个粘性应用程序,为此我编写了两个组件。一个是容器,它渲染其中的每个粘性组件,第二个使用从 JSON 提供给它的数据。
class App extends React.Component {
state = {
notes: tasks
}
render(){
return <Stickycontainer tasks={this.state.notes}/>
}
}
class Stickycontainer extends React.Component{
render(){
return this.props.tasks.map(e => <Stickynote data={e}/>)
}
}
这个片段工作得很好,因为我尝试而不是渲染一个 Stickynote,而是只渲染一个带有每个对象及其数组属性的段落元素。但是当我尝试在下面实现 Sticky 组件时出现错误:
class Stickynote extends React.Component{
state = {
show: true
}
render(){
if(this.state.show){
return(
<div className="StickyNotes">
<p>{this.props.data.id} - {this.props.data.description} - {this.props.data.title}</p>
<div className="Actions">
<button className="Delete" onClick={() => this.setState({show: false})}>
<Icon>delete</Icon>
</button>
</div>
</div>
);
} else {
return null;
}
}
}
它给了我这个错误:
错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。
检查渲染方法
App
我该怎么办?
解决方案
推荐阅读
- c# - 如何使用贝叶斯服务器为动态贝叶斯网络中的变量指定初始概率值
- encryption - 使用 JetBrains Exposed 时加密字段
- javascript - Json 数据未呈现到 DOM
- r - 在列中拆分字符串并在 R 中添加重复的行
- generics - Kotlin out-projected type prohibits the use
- api - 网址格式:
- sublimetext3 - 如何使 sublime 换行支持半角和全角字符?
- django - Apache、Gunicorn 和 Django 的服务器错误
- python - 为什么我们需要在实例尚未创建时在 __init__ 构造函数中指定 self ?
- java - Java继承是不是抄了Super内容?