So I have this wrapper component
const AccessControl = ({ content, children }: acProps) => {
return content ? children : null;
};
And I have used it like this
<AccessControl content={false}>
{console.log("Ashish")}
<div>This is executing</div>
</AccessControl>
So it is executing the children even the content is false. If this is how it works, how can I use a wrapper component to avoid ternary operators?
4
Answers
JS evaluates its arguments before calling a function, including JSX expressions before they are passed as children to a component – which is why with
content={false}
the<div>This is executing</div>
is not showing up, yet you can still see theconsole.log
.To get around this you can pass
children
as a function like this:and call
children
inAcessControl
component (remember to change type inacProps
fromReactNode
to() => ReactNode
)Your wrapper component does do its job, the children won’t be rendered to the DOM, it’s just that functions inside a component will be run when it is rendered, which is why the console.log is running.
But a better way to go about this is to just conditionally render it via short circuiting.
{content && (<div>This is executing</div>)}
You can refactor the component into a wrapper component that takes multiple children and renders them based on their type. This approach is based on the concept of compound components, which are a pattern for creating more flexible and reusable components.
Example:
In the above code, The
AccessControl
is a wrapper component that takes multiple children. It uses theReact.Children.map
function to iterate over its children and render them based on their type and a condition prop.The
AccessControl.Content
component is a special child component that takes a condition prop and its own children. If the condition prop is truthy, it renders its children; otherwise, it renders null.After that, you can use this refactored
AccessControl
component like this:This will allows you to avoid using a
ternary operatorand makes your code more flexible and maintainable. It also provides a clear and explicit way to control the rendering of different parts of your component.This is a suggestion, so if not usefull, please tell me so I will remove this answer immediately. 🙂
Because it’s an inline code and always runs despite of condition it
true
orfalse
.In React typically we use inline code only for conditional rendering. You can’t use any business logic there if you want try this.
Or even better without any logic just for rendering like this
Using Children is uncommon and can lead to fragile code More on