1

Can someone help me render react components with variable names? For example, I have a component that imports 3 other components and I want to loop through the array of components and render them. Very rough pseudocode:

import component1 from '...';
import component2 from '...';
import component3 from '...';

const component_list = ['component1', 'component2', 'component3']

renderComponents() {
    return this.component_list.map((component) {
      <div>
        <{component} />
      </div>
    });
}
1
  • I was going to say your already their expert for the curly braces. Gj Commented Dec 27, 2016 at 17:16

2 Answers 2

2

Update the list of components with the components classes (rather than strings) and note that custom components must be capitalised:

import component1 from '...';
import component2 from '...';
import component3 from '...';

const component_list = [component1, component2, component3];

renderComponents() {
    return this.component_list.map((Component) => {
      <div>
        <Component />
      </div>
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0
import component1 from '...';
import component2 from '...';
import component3 from '...';

const component_list = [component1, component2, component3]

renderComponents() {
    return component_list.map((component) => {
      <div>
        <component />
      </div>
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.