I want to have a const style and inline hardcoded style to my element lets say I have this:
const style = {
cursor: "pointer",
border: "2px solid #b1b1b1",
borderRadius: "8px",
padding: "0px 10px 0px 10px",
backgroundColor: "green"
}
But I also want additional style to the element lets say width
<div style={style{width: "20%"}}>test</div>
This is not working, how can I achieve this?
You can use the spread syntax for assigning your styles.
<div style={{...style, ...{width: "20%"}}}>test</div>
const style = {
cursor: "pointer",
border: "2px solid #b1b1b1",
borderRadius: "8px",
padding: "0px 10px 0px 10px",
backgroundColor: "green"
}
const newStyles = {...style, ...{width: "20%"}};
console.log(newStyles);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With