I have written apps in react native before, and I am about to start my first react project. I noticed a tool called Styled Components: https://www.styled-components.com/docs/basics#motivation
However, I don't see any clear benefits of it except the fact that I can do media queries within the style definitions all within the same file as my component.
But lets say I have this button:
import React from 'react';
const StyledButton = ({ children }) => {
return (
<button type="button" style={styles.button}>
{ children }
</button>
);
}
const styles = {
button: {
backgroundColor: '#6BEEF6',
borderRadius: '12px',
border: 'none',
boxShadow: '0 5px 40px 5px rgba(0,0,0,0.24)',
color: 'white',
cursor: 'pointer',
fontSize: '15px',
fontWeight: '300',
height: '57px',
width: '331px',
}
}
export default StyledButton;
How would writing this in styled-components be much different? Is it only the case where I have certain styles that depend on certain props
that styled-components shines?
Example, this doesn't work in react:
const StyledButton = ({ children, primary }) => {
return (
<button type="button" style={[styles.button, { color: primary ? 'green' : 'red' }]}>
{ children }
</button>
);
}
style={Object.assign(styles.button, { color: primary ? 'green' : 'red' })}
– Jeroen Wienk