0

I have to render different components based on:

  • if the items contain a certain value
  • and if all 10 items have that value
const MyComponent = ({ data }: Props) => {
  const filteredItems = data.items?.filter(
    (item) => item?.type.includes(MyType.Value1) || item?.type.includes(MyType.Value2)
  );

  filteredItems.map((item) => {
    if (item.type.includes(MyType.Value1)) {
      return (
        <div>
          <Component one />
        </div>
      );
    }
    return (
      <div>
        <Component two />
      </div>
    );
  });
  return null;
};

export default MyComponent;

1 Answer 1

1

1/ if the items contain a certain value

(data?.items || []).find(item => [your condition here]) && <ComponentToRenderIfTrue />

2/ if all 10 items have that value

(data?.items || []).every(item => [your condition here]) && <ComponentToRenderIfTrue />
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, how should I bake that into my example?
Does this work in a retun as a jsx expression? for example return( {(item?.items || []).find(item => [contains('grid-cols')]) && <GridCols ItemType={TextBlockItem} scheme={scheme} gridCols={item?.[grid-cols-three]}/> } )
Of course! No reason it would not work :)

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.