0

So in JSX I know how I can use an conditional on class names etc or where the attribute has a value.

So if I'm doing class="something" or style="something" etc. The issue I'm having is i'm unsure how I'd do a conditional when adding the hidden attribute.

Example;

  className={classNames({
    'hidden': true,
    'visible': props.showAll,
  })}

This will add a class depending on the value of showAll.

But the hidden attribute is literally just <p hidden>this text is hidden</p>.

How would I go about adding a conditional into my JSX react component that renders an attribute but no value?

2 Answers 2

3

Simply try with:

<p hidden={!showAll}>

Attribute hidden with no value is concidered as true, so you can easily pass a boolean value.

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

1 Comment

Ahh so hidden="" returns true, the same as just adding the hidden attribute. Thanks!
0

If the data is present then show otherwise do not append the p tag on dom.So try this

{this.props.showAll && <p>this.props.showAll</p>}

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.