To use styled-components in React, follow these steps:
npm install styled-components
import styled from 'styled-components';
const StyledButton = styled.button`
background-color: blue;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: lightblue;
}
`;
<StyledButton>Click me</StyledButton>
const StyledInput = styled.input`
border: 1px solid ${props => props.error ? 'red' : 'green'};
`;
Then use the component and pass in props:
<StyledInput error />
That's it! You have successfully used styled-components in your React application. This approach allows you to write CSS directly inside your component files and easily manage styles.