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

React Hook How to pass styles

I wonder if there is a simpler way to pass the default style property (as it is a default property) to a custom component. I hope someone can just tell me how to improve the following code.

Given:

const MyIcon= ()=>{
  return <img src={GMNT_N_Black}/>
}

The following style property is not being applied.

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

<MyIcon style={{width:"1em",height:"1em"}}/>

I’d like to pass the default property style to the img element in MyIcon.

I made it work with the following code:

const MyIcon = ({style})=>{
  return <img src={imported_img} style={style}/>
}

However, I suspect there should be an easier and cleaner way to pass the style, especially since img is the only element inside. Does exist this ‘simpler’ way or is {style} as props the proper way in this situation?

>Solution :

You can use it like this

<MyIcon style={{width:"1em",height:"1em"}}/>

    const MyIcon = (props)=>{
      return <img src={imported_img} {...props}/>
    }

Whatever property you pass will be applied automatically

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