0

I have a question about rendering components from object map. I have a structure like this:

const tabs = [
    'tab1': {
        name: 'Tab 1',
        renderComponent: () => <Tab1Component />
    },
    'tab2': {
        name: 'Tab 2',
        renderComponent: () => <Tab2Component />
    }
];

I want to access the renderComponent property and render sub component. Tried with function invocation, React.createElement(), doesn't work.

const MyComponent = ({tabs}) => {
    const activeTab = 'tab1';
    return (
        <>
        // How to render it?
        // function invocation?
        // createElement??
            tabs[activeTab].renderComponent();
        </>
    );
};
3
  • 2
    adding curly braces should do the trick: { tabs[activeTab].renderComponent() } Commented Feb 20, 2020 at 12:18
  • @PatrickHund I actually have written it like that, but forgot to add it to this mocked examples. Still it doesn't work. it says: tabs[activeTab].renderComponent is not a function Commented Feb 20, 2020 at 12:26
  • Yeah, I didn't spot the square brackets in the declaration of tabs, those should be curly. atadnmz's answer is correct. Commented Feb 20, 2020 at 13:27

3 Answers 3

1

Your array declaration is wrong. you should use object or array that includes objects. Try this block.

const tabs = {
  tab1: {
    name: "Tab 1",
    renderComponent: () => <Tab1Component />
  },
  tab2: {
    name: "Tab 2",
    renderComponent: () => <Tab2Component />
  }
};

Also surround your js code with {} like;

const MyComponent= () => {
  const activeTab = "tab1";
  return <>{tabs[activeTab].renderComponent()}</>;
};
export default MyComponent;
Sign up to request clarification or add additional context in comments.

Comments

0

Here things i guess you did wrong is
1) adding the single quotes around the object-key
2) writing key-value pairs in array without enclosing it in {}

I think you should do something like this

const tabs = [
        {
            tab1: {
                name: 'Tab 1',
                renderComponent: () => <Tab1Component />
            },
            tab: {
                name: 'Tab 2',
                renderComponent: () => <Tab1Component />
            }
        }
    ];

    const activeTab = 'tab1';

and

const MyComponent = ({tabs}) => {
    const activeTab = 'tab1';
    return (
        <>
        // How to render it?
        // function invocation?
        // createElement??
            tabs[0][activeTab].renderComponent();
        </>
    );
};

Comments

0
const MyComponent = () => {

const activeTab = 'tab2';

const tabs = [
{'tab1': {
    name: 'Tab 1',
    renderComponent: () => <Tab1Component />
}},
{'tab2':{
    name: 'Tab 2',
    renderComponent: () => <Tab2Component />
}}

];

  return (
  <div>
    {tabs.map((el) => {
      if (el[activeTab]) {
        return el[activeTab].renderComponent()
      }
    })}
  </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.