【发布时间】:2017-03-04 17:21:46
【问题描述】:
我需要将组件的props存储在一个变量中后,这里是伪代码:
render(){
let items = [{title:'hello'}, {title:'world'}];
let component = false;
switch (id) {
case 1:
component = <A />
break;
case 2:
component = <B />
break;
}
return(
items.map((item, index)=>{
return(
<span>
{/* SOMETHING LIKE THIS WOULD BE COOL - IS THAT EVEN POSSIBLE*/}
{component.props.set('title', item.title)}
</span>11
)
})
)
}
在return 内部,我运行一个循环,我需要为存储在变量中的组件设置道具...。如何为我之前存储在变量中的该组件设置道具?
【问题讨论】:
-
在渲染过程中不能改变 state 或 prop,可以使用 componentDidUpdate 或 componentWillReceiveProps 来实现
-
在这里克隆组件是一个不好的选择,因为它会为克隆的组件创建额外的内存来影响性能。并且,克隆后的原始组件变得无用。同样,这是一种糟糕的实现方式。