I have a React component:
const Options = () => {
// Option logic
if (isOptionOne) {
return <OptionOne />;
}
if (isOptionTwo) {
return <OptionTwo />;
}
if (isOptionThree) {
return <OptionThree />;
}
return null;
};
I render this component inside a parent component:
const ParentComponent = () => {
<ComponentA />
<span css={styles.container}
<Options />
</span>
};
What is a clean approach to move the <span>
tag to the child component (<Options />
), since I want to render this html tag only if <Options />
component does not return null.
2
Answers
You should check if Options component returns null or the component. If the component is returned then render, else don’t.
You can use Boolean() for checking if it returns null or component.
if it returns false then don’t render html tag if it does then render.
The code looks like this:
You can simply do it inside each condition:
There is no need to overthink this. This is clean enough.