我需要在组件存储在变量中之后设置它的道具,这里是伪代码:
@H_404_1@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
)
})
)
}
在内部返回我运行一个循环,我需要为存储在变量中的组件设置道具….如何设置我之前存储在变量中的组件的道具?
正确的方法是使用React的cloneElement方法(
https://facebook.github.io/react/docs/react-api.html#cloneelement).
您可以通过以下方式实现您想要的目标: @H_404_1@<span> { React.cloneElement( component,{title: item.title} ) } </span>
您可以通过以下方式实现您想要的目标: @H_404_1@<span> { React.cloneElement( component,{title: item.title} ) } </span>