-1

I'm using my components as below and I want to check if one of inner components is returning anything or not to make style decisions based on that.

<Grid xs={12} md={ Bar() ? 8 : 12 }>
  <Foo />
</Grid>
<Grid xs={12} md={4}>
  <Bar />
</Grid>
2
  • 2
    There is :empty CSS selector, so you can hide that element. Commented Jul 1, 2024 at 8:42
  • @Justinas Thanks, but I just made my intention clearer as it's not only about visuals of the page. I want to know if it's empty or not inside my JSX file to be able to take different decisions based on that. Commented Jul 1, 2024 at 8:50

1 Answer 1

1

Usually state elements can be introduced and set based on the same condition that you render the child (DashboardLongTermSuggestedPackages in this case). So that you can use the state element everywhere (isVisible) and render the remaining UI logic. ex : create [isVisible, setIsVisible] in parent, then pass and setIsVisible inside the child. So the Parent is aware of the current state.

Also, one more way, if you want to know if there is a child for a parent you can use props.children to verify.

<Parent> <Child/> </Parent>

then inside Parent, you can use props.children conditionally.

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

1 Comment

I want to find if a certain child element exist in a parent which has multiple children like: <div> <span>Hello</span> <span> <Bye /> </span> </div>

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.