简体   繁体   English

在 React 中将一组组件作为道具传递

[英]Passing an array of Components as a Props in React

I am looking for a way to pass an array of components to a prop for a tabbing component.我正在寻找一种将组件数组传递给选项卡组件的道具的方法。 Just wondering if that's possible.只是想知道这是否可能。

So I need to create a component that will shorten material ui's tabbing method but I cannot find a way to pass an array of components as a prop so that it will be rendered on that component.所以我需要创建一个组件来缩短 Material ui 的 tabbing 方法,但我找不到一种方法来传递组件数组作为 prop 以便它会在该组件上呈现。

Here is an example of my code:这是我的代码示例:

<FullWidthTab
components = [<Component1/>, <Component2/>] //this is where components renders
 menuLabels = ['Menu1', 'Menu2'] //this is where title render
/>

And I map them on my code like this and I used lodash map method:我像这样将它们映射到我的代码上,并使用了 lodash map 方法:

map(components, component=>{

 <TabContainer>{component}</TabContainer>

}

But it returns this.但它返回这个。 Warning: react-swipeable-view: one of the children provided is invalid: null.警告:react-swipeable-view:提供的孩子之一无效:null。 We are expecting a valid React Element我们期待一个有效的 React 元素

And when I console.log the component it returns: {$$typeof: Symbol(react.element), type: ƒ, key: null, ref: null, props: {…}} Object Need help当我console.log组件返回:{$$typeof: Symbol(react.element), type: ƒ, key: null, ref: null, props: {...}} Object Need help

I hope it can render the components.我希望它可以渲染组件。

Cause you need return a value in map. 因为您需要在地图中返回一个值。

map(components, component=>{

 return <TabContainer>{component}</TabContainer>

}

Component's name must be started with a capital letter, but I can see here components = [<component1/>, <component2/>] they were not. 组件的名称必须以大写字母开头,但是我在这里可以看到, components = [<component1/>, <component2/>]不是。 So you must convert [<component1/>, <component2/>] to [<Component1/>, <Component2/>] . 因此,您必须将[<component1/>, <component2/>][<Component1/>, <Component2/>] Second thing I see your map syntax is strange, it must be like this: 第二件事,我看到您的map语法很奇怪,它一定是这样的:

components.map(component => (<TabContainer>{component}</TabContainer>))

Reference: https://reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized 参考: https : //reactjs.org/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized

Your es6 syntax needs a subtle adjustment to return the react component. 您的es6语法需要进行细微调整才能返回react组件。

map(components, component => <TabContainer>{component}</TabContainer> )

or, if you can use the map function from your array instead of importing from a library. 或者,如果您可以使用数组中的map函数而不是从库中导入。

components.map( component => <TabContainer>{component}</TabContainer> )

I solved This with another implementation. 我用另一个实现解决了这个问题。

I used 'react-swipeable-views' and instead passing a component I used this 我使用了“ react-swipeable-views”,而是传递了一个我用过的组件

       <SwipeableViews
            axis={theme.direction === 'rtl' ? 'x-reverse' : 'x'}
            index={this.state.value}
            onChangeIndex={this.handleChangeIndex}
        >
            { children }
        </SwipeableViews>

And Changed: 并更改:

<FullWidthTab
components = [<Component1/>, <Component2/>] //this is where components renders
 menuLabels = ['Menu1', 'Menu2'] //this is where title render
/>

To

<FullWidthTab
 components = [<Component1/>, <Component2/>] //this is where components renders
 menuLabels = ['Menu1', 'Menu2'] //this is where title render
>
  <Component1/>
  <Component2/>
</FullWidthTab>

But if you can get how to pass components via props that would be great! 但是,如果您可以获得如何通过道具传递组件的话,那就太好了!

I think you have to pass the component on array for FullWidthTab as a node value: 我认为您必须将FullWidthTab数组上的组件作为节点值传递:

<FullWidthTab
components = [component1, component2] //this is where components renders
 MenuLabels = ['Menu1', 'Menu2'] //this is where title 
render
/>

And your map continue as it is. 您的地图继续原样。

If this not work, try to render the component inside the map. 如果这不起作用,请尝试在地图内渲染组件。 Pass the component as node for FullWidthTab, and then render it: 将组件作为FullWidthTab的节点传递,然后呈现它:

map(components, component=>{

 <TabContainer>{<component/>}</TabContainer>

}

Note: this code wasn't tested! 注意:此代码未经测试!

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

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