0

I am using Atomic design to structure my React components.

A certain ui child component (an atom) should be shown or not based on a value (prop).

Is it a better practice to add the conditional rendering inside this child component like:

const Child = ({ show }: Props) => {
  return show ? (
    <span>Foo</span>;
  ) : null
}

or add the conditional rendering part inside the parent component:

{ show && <Child /> }

What is a better practice: Add conditional rendering logic inside React child ui component or inside parent component?

2 Answers 2

0

Everything depends on what you want to achieve. Basically there is no significant difference between these two ways of conditional rendering when it comes to performance or something.

The question is: do you want to conditionaly render Child at all or make Child configurable and control span rendering inside it? It's more like architectural/design decision than code quality or good practice problem.

Anyway... There is no one golden rule. It depends on what you need in your project.

Sign up to request clarification or add additional context in comments.

Comments

0

Second approach is better since it would be readable code and also avoids rendering the Child component in case 'show' is falsy.

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.