【问题标题】:Can I store a component in a variable and push children to it after it has been created?我可以将组件存储在变量中并在创建后将子组件推送到它吗?
【发布时间】:2022-08-18 19:58:44
【问题描述】:

可以说我有这个组件:

const Test = ({ children, ...rest }) => {
    return <>{children}</>
};

export default Test;

我想知道是否可以创建一个保存组件的变量,如下所示:

const test = <Test></Test>;

然后循环遍历一些数据并在每次迭代时将子项推送到test 变量。

  • 渲染&lt;Test&gt;{arr.map(...)}&lt;/Test&gt; 会起作用吗?你会推送到arr(当然不要推送,因为它会改变arr,但会连接到它),然后在.map()内你会返回JSX

标签: javascript reactjs react-native


【解决方案1】:

是的,试试这个模式:

const test = (children) => <Test>{chidlren}</Test>;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-30
    • 2022-11-30
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多