最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - Render dynamic list of components - Stack Overflow

programmeradmin1浏览0评论

I have a list of keys which represent a custom React component. Based on this list I want to render the appropriate component. I have a reference to each component so I can create a map of key -> Component which allows me to create a list of my components. However I have not found a way to render this list.

Example.:

input: ["componentA", "componentB", "componentC"]

output:
<ComponentA />
<ComponentB />
<ComponentC />

This is what I got so far, however I'm not sure how to render the list of components:

function renderElements(keys) {
  const components = {
    componentA: ComponentA,
    componentB: ComponentB,
    componentC: ComponentC,
  };

  const componentsToRender = keys.map(key => components[key]);

  return (
    <div>
      {componentsToRender}
    </div>
  );
}

I have a list of keys which represent a custom React component. Based on this list I want to render the appropriate component. I have a reference to each component so I can create a map of key -> Component which allows me to create a list of my components. However I have not found a way to render this list.

Example.:

input: ["componentA", "componentB", "componentC"]

output:
<ComponentA />
<ComponentB />
<ComponentC />

This is what I got so far, however I'm not sure how to render the list of components:

function renderElements(keys) {
  const components = {
    componentA: ComponentA,
    componentB: ComponentB,
    componentC: ComponentC,
  };

  const componentsToRender = keys.map(key => components[key]);

  return (
    <div>
      {componentsToRender}
    </div>
  );
}
Share Improve this question edited May 27, 2018 at 13:49 fwind asked Apr 24, 2017 at 10:35 fwindfwind 1,3144 gold badges15 silver badges32 bronze badges 3
  • Show us the code that you have so far to try and make this happen. – Brett East Commented Apr 24, 2017 at 10:41
  • You need to map array and return component. Not much logic I guess. – Ved Commented Apr 24, 2017 at 10:42
  • component should be in capital.like this: ["ComponentA", "ComponentB", "ComponentC"] – Ved Commented Apr 24, 2017 at 10:45
Add a comment  | 

4 Answers 4

Reset to default 9

What worked for me:

render() {
  const components = [ComponentA, ComponentB, ComponentC] // references to components
  return (
    <div>
      {components.map((comp, i) => React.createElement(comp, { key: i })}
    </div>
  );
}

Had to use a reference to the component and had to use React.createElement due to problems with nested JSX (might be Typescript related).

Try this. It should work.

 render()
    {
      const input=["ComponentA", "ComponentB", "ComponentC"]

       return(
             <div>
               {
                  input.map((comp,i)=>){
                      return <{comp} key={i} />
                    }
                }
             </div>

        )
    }

Note: input= ["ComponentA", "ComponentB", "ComponentC"];Custom component name should be in Capital

render() {
    return (
       <div>
          {componentsToRender.map((Component, key) => (<Component key={key}/>))}
       <div>
    );
  }

For some reason using the map function as specified in the React documentation didn't work for me for any component/element that's not an li, I had to explicitly specify a return statement.

options = optionsArray.map(o => {
    // replace option with your component name
    return <option key={o.name} value={o.name}>{o.name}</option>
})

then I rendered the generated elements. You can do it inline too if you want.

<div>
    {options}
</div>
发布评论

评论列表(0)

  1. 暂无评论