【问题标题】:Next js pass props down when using children下一个js在使用children时向下传递props
【发布时间】:2020-09-17 22:44:54
【问题描述】:

我在 Next js 页面中有一个这样的组件:

 ...
     <Chart
        isShoppingChartOpen={isShoppingChartOpen}
        toggleShoppingChart={toggleChartVisibility}
        lineItems={lineItems}
      />

      <main className="relative">
        <div>{children}</div>
      </main>
...

我也需要将toggleChartVisibility 传递给孩子们。有没有办法做到这一点?

【问题讨论】:

    标签: javascript reactjs next.js


    【解决方案1】:

    如果你想将它作为道具传递给每个孩子你可以尝试这样的事情:

    
     children = children.map((el) => {
        let props = el.props;
        props.toggleChartVisibility = "Value";
        return <ChildrenComponent props={props} children={el.children}/>
      });
    
    
    

    很遗憾,您不能在创建子元素后直接更改它们的属性。因此,最好在创建孩子时将所需的道具插入到孩子中。

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2021-12-25
      • 2016-02-04
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多