0

In my div, I have a function which renders components based on what is present in an object. However, even though cat.comp returns the actual functional component, it does not get rendered.

This is my render function:

{cardsInGrid.map((card) => {
            sideBarCategories.map((cat) => {
              if (card.id === cat.id) {
                const Component = cat.comp;
                return <Component key={card.id} />;
              }
            });
          })}
1
  • Return the inner map or remove the bracket's from its parent arrow function Commented May 21, 2020 at 1:04

2 Answers 2

2

Nothing is being returned from the first map. You need to return the array of elements:

          {cardsInGrid.map((card) => {
            return sideBarCategories.map((cat) => {
              if (card.id === cat.id) {
                const Component = cat.comp;
                return <Component key={card.id} />;
              }
            });
          })}

However, this is going to result in a two-dimensional array so you'll need to flatten that. One approach would be to move this out of the component tree and create a one-dimensional array. For example, something along the lines of:

const elements = [];

cardsInGrid.forEach(card => {
  sideBarCategories.forEach(cat => {
    const Component = cat.comp;
    elements.push(<Component key={card.id + cat.id} />);
  });
});

return <>{elements}</>;

After further discussion, it sounds like you only ever have one component and you want to find that specific category and render it. In that case, I would suggest using find (or equivalent logic). For example:

{cardsInGrid.map(card => {
  const Component = sideBarCategories.find(cat => cat.id === card.id).comp;
  return <Component key={card.id} />;
})}
Sign up to request clarification or add additional context in comments.

4 Comments

I didn't look so detailed to the code, so good catch :) I don't know what OP wants but I'm upvoting your answer.
@skovy I have an map with an id and the corresponding component and what I am trying to do is get the ids listed in the cardsInGrid array and find the corresponding component which is listed with that same id in sideBarCategories. What would be the most efficient way of doing that?
@NathanealVarghese I updated the answer, I would suggest using find. Is this the problem you're trying to solve?
@skovy That works flawlessly! Thank you so much, I learned about the .find function!
1

You are missing the return in your first map method. You should add it:

{
  cardsInGrid.map((card) => {
    return sideBarCategories.map((cat) => {
      if (card.id === cat.id) {
        const Component = cat.comp;
        return <Component key={card.id} />;
      }
    });
  });
}

or you can use implicit return:

{
  cardsInGrid.map((card) =>
    sideBarCategories.map((cat) => {
      if (card.id === cat.id) {
        const Component = cat.comp;
        return <Component key={card.id} />;
      }
    })
  );
}

2 Comments

Thank you for that. The return in front of it worked. Do you have any way of simplifying the function itself?
Well, it seems pretty straightforward to me :) If the result is what you want then go ahead. Also, you can check @skovy's answer.

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.