简体   繁体   English

React-传递独特的React组件作为道具

[英]React - passing unique React components as props

I'm interested in understanding the best practice when it comes to passing down React components as a prop, particularly when passing unique components. 我有兴趣了解将React组件作为道具传递时的最佳实践,特别是在传递唯一组件时。

Some example pseudocode 一些伪代码示例

import IconOne;
import IconTwo;
import IconThree;

const arr = [
    { icon: IconOne },
    { icon: IconTwo },
    { icon: IconThree },
]

render() {
    return (
        {arr.map(item => (
            <Container icon={item.icon} />
        ))}
    )
}

The icons I have are unique, in the sense that they render an svg icon. 从某种意义上说,我拥有的图标呈现了svg图标。 Is it bad practice to do so? 这样做是不好的做法吗?

There is nothing wrong with your example, but render functions are currently quite a trend. 您的示例没有错,但是渲染功能目前已成为一种趋势。 Presumably for the additional level of control via props passing and lazy evaluation. 大概是通过道具传递和惰性评估来实现额外的控制级别。 Also: don't forget to pass keys . 另外: 不要忘记传递密钥

import IconOne;
import IconTwo;
import IconThree;

const arr = [
    { title: 'one', render: props => <IconOne {...props} /> },
    { title: 'two', render: props => <IconTwo {...props} /> },
    { title: 'three', render: props> <IconThree {...props} /> },
]

render() {
    const foo = this.props.foo
    return arr.map( ({ title, render }) => 
        <Container key={title} renderIcon={() => render(foo)} />
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM