Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I conditionally add attributes to components?

trying to add some eleemnts to exist style properties,
how to destruct and add some elements conditionally

retun <div style={{...FULL, screenMinimized ? overflow: 'auto' : display:'none' }}>

...
const FULL: React.CSSProperties = {
  flex: 1,
  display: 'flex',
  flexDirection: 'column',
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use the ternary on the property value you are trying to set/override in the style prop object.

Example:

<div
  style={{
    ...FULL,
    overflow: screenMinimized ? 'auto' : 'hidden',
  }}
>
  ...
</div>

If you want to conditionally set different properties though you’ll need to create each as a separate object to be spread into the style prop object:

<div
  style={{
    ...FULL,
    ...(screenMinimized ? { overflow: 'auto' } : { display: 'none' })
  }}
>
  ...
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading