if ProjectItem don't have to reuse anywhere, where is the difference?
and what if i put declare component inside a loop, does it consume lots of memory?
1
const ProjectItem = ({ _id, title }) => {
return <div key={_id}>
<a href={`/projects/${_id}`}>{title}</a>
</div>
}
class view extends Component {
render() {
const { ready, projects } = this.props
if (!ready)
return <LoadingView />
return projects.map((project, projectIdx) => <ProjectItem key={projectIdx} {...project} />)
}
}
2
class view extends Component {
render() {
const { ready, projects } = this.props
if (!ready)
return <LoadingView />
const ProjectItem = ({ _id, title }) => {
return <div key={_id}>
<a href={`/projects/${_id}`}>{title}</a>
</div>
}
return projects.map((project, projectIdx) => <ProjectItem key={projectIdx} {...project} />)
}
}
3
class view extends Component {
render() {
const { ready, projects } = this.props
if (!ready)
return <LoadingView />
return projects.map((project, projectIdx) => <ProjectItem key={projectIdx} {...project} />)
function ProjectItem({ _id, title }) {
return <div key={_id}>
<a href={`/projects/${_id}`}>{title}</a>
</div>
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…