Reactjs 在react中将组件定义数组作为道具传递

Reactjs 在react中将组件定义数组作为道具传递,reactjs,react-native,Reactjs,React Native,我有兴趣将react组件s数组作为道具 export default class Table extends React.Component { render() { return ( ... {this.props.actions ? <td>{ this.props.actions.map((f, k) => f(r, k)) }</

我有兴趣将react
组件
s数组作为
道具

export default class Table extends React.Component {
    render() {
        return (
            ...
            {this.props.actions ? <td>{
                this.props.actions.map((f, k) => f(r, k))
                }</td> : ''}
            ...
        )
    }
}


const actions = [
    (row, index) => <RemoveTableAction row={row} key={index}/>
    (row, index) => <EditTableAction row={row} key={index}/>
]

<Table ... actions={actions} />

如何在
表中实例化这些组件

map((CompDef, k) => <CompDef key={k} row={r} />)
map((CompDef,k)=>)
确保映射内的CompDef变量以大写字母开头

编辑

这里有一个更好的解决方案,可以为这两个组件提供灵活性(小心使用)

const actions=[,];
在Table.js中:

export default class Table extends React.Component {
    render() {
        return (
            ...
            {this.props.actions ? <td>{
                this.props.actions.map(
                    (action, k) => 
                        react.cloneElement(action, {row: r, key: k})
                )
            }</td> : ''}
            ...
        )
    }
}
导出默认类表扩展React.Component{
render(){
返回(
...
{this.props.actions{
这个.props.actions.map(
(动作,k)=>
cloneElement(操作,{row:r,key:k})
)
} : ''}
...
)
}
}
这样我们就可以克隆元素并覆盖或添加一些道具。 不,没有严重的性能开销


要获得更详细的文档,请查看。

就像您正在做的一样

map((CompDef, k) => <CompDef key={k} row={r} />)
map((CompDef,k)=>)
确保映射内的CompDef变量以大写字母开头

编辑

这里有一个更好的解决方案,可以为这两个组件提供灵活性(小心使用)

const actions=[,];
在Table.js中:

export default class Table extends React.Component {
    render() {
        return (
            ...
            {this.props.actions ? <td>{
                this.props.actions.map(
                    (action, k) => 
                        react.cloneElement(action, {row: r, key: k})
                )
            }</td> : ''}
            ...
        )
    }
}
导出默认类表扩展React.Component{
render(){
返回(
...
{this.props.actions{
这个.props.actions.map(
(动作,k)=>
cloneElement(操作,{row:r,key:k})
)
} : ''}
...
)
}
}
这样我们就可以克隆元素并覆盖或添加一些道具。 不,没有严重的性能开销


有关更详细的文档,请参阅。

您可以使用react-children或RenderProps。您可以使用react-children或RenderProps。这是有效的。但是我想我必须坚持前面的解决方案,因为它给了你更多flexibility@tzortzik我添加了另一个解决方案,我想您会使用它:))这很有效。但是我想我必须坚持前面的解决方案,因为它给了你更多flexibility@tzortzik我添加了另一个解决方案,我想您会使用它:))