Skip to main content
Add more information
Source Link
Neil Girardi
  • 5k
  • 3
  • 35
  • 47

Given a local variable isRequired I believe youYou can do the following in your render method (if using a class) or return statement (if using a function component):

 <MyComponent required={isRequired ? 'true' : undefined} />

In this case, the attribute will not be added if isRequired is undefined, false, or null (which is different from adding the attribute but setting it to 'false'.) Also note that I am using strings instead of booleans in order to avoid a warning message from react (Boolean value received on non-boolean attribute).

Given a local variable isRequired I believe you can do the following in your render method (if using a class) or return statement (if using a function component):

 <MyComponent required={isRequired ? 'true' : undefined} />

Given a local variable isRequired You can do the following in your render method (if using a class) or return statement (if using a function component):

 <MyComponent required={isRequired ? 'true' : undefined} />

In this case, the attribute will not be added if isRequired is undefined, false, or null (which is different from adding the attribute but setting it to 'false'.) Also note that I am using strings instead of booleans in order to avoid a warning message from react (Boolean value received on non-boolean attribute).

Source Link
Neil Girardi
  • 5k
  • 3
  • 35
  • 47

Given a local variable isRequired I believe you can do the following in your render method (if using a class) or return statement (if using a function component):

 <MyComponent required={isRequired ? 'true' : undefined} />