How to use style in ReactJS
Understanding Styles in ReactJS
Styles in ReactJS are implemented in a unique way that might be a bit different from what you're used to in HTML and CSS. They're integrated directly into the components themselves, which makes for a more unified and streamlined approach.
What is a Component?
Before we jump into styling, let's take a moment to understand what a component is in ReactJS. A component is like a building block. Just like how a house is built with bricks, a ReactJS application is built with components. Each component represents a part of the user interface (UI). For example, you might have one component for a button, another for an input field, and so on.
Inline Styles
The simplest way to add styles to your components in ReactJS is by using inline styles. "Inline" means that the styles are written directly within the component. Here is an example:
const MyComponent = () => {
return (
<div style={{ color: 'blue', fontSize: '20px' }}>Hello, world!</div>
);
};
In this example, the style
attribute is added to the div
element with a JavaScript object within curly braces {}
. Each key-value pair represents a CSS style, where the key is the CSS property and the value is the CSS value. Note that the CSS properties are written in camelCase, not kebab-case.
CSS in JS
Another way to style your components in ReactJS is by using a technique called CSS-in-JS. This involves writing your CSS styles in a JavaScript file and then importing them into your component. Here's an example:
// styles.js
export const styles = {
container: {
color: 'blue',
fontSize: '20px'
}
};
// MyComponent.js
import { styles } from './styles';
const MyComponent = () => {
return (
<div style={styles.container}>Hello, world!</div>
);
};
In this example, the styles are written in a separate JavaScript file and then imported into the component file. This allows you to reuse the same styles across multiple components, which can make your code more DRY (Don't Repeat Yourself).
Styled Components
Styled-components is a library that allows you to write actual CSS code to style your components. It's a more advanced technique, but it can make your styles more organized and easier to manage.
Here's how you could use styled-components to style the same MyComponent
:
// MyComponent.js
import styled from 'styled-components';
const Container = styled.div`
color: blue;
font-size: 20px;
`;
const MyComponent = () => {
return (
<Container>Hello, world!</Container>
);
};
In this example, we first import the styled
function from the styled-components library. Then, we create a styled component by calling styled.div
and passing in a template literal with our CSS code. Finally, we use our styled component Container
in place of the regular div
.
The Power of CSS Modules
CSS Modules is a technique that allows you to write CSS in its own file and then import it into your JavaScript file as an object. This is similar to CSS-in-JS, but with the benefit of keeping your CSS in a separate file.
Here's an example of how you might use CSS Modules:
// styles.module.css
.container {
color: blue;
font-size: 20px;
}
// MyComponent.js
import styles from './styles.module.css';
const MyComponent = () => {
return (
<div className={styles.container}>Hello, world!</div>
);
};
In this example, we first create a CSS file with the .module.css
extension. Then, we import it into our component file as an object. Finally, we use the imported styles in our className
attribute.
The Final Touch
To style or not to style, that's not a question in ReactJS. With the various styling techniques, you can give your application the look and feel you desire. Whether you choose to use inline styles, CSS-in-JS, styled-components, or CSS Modules, the choice is yours. Remember, the best styling technique is the one that makes your code clean, organized, and easy to understand.
Just like a fashion designer selects and combines various elements to create a stunning outfit, as a ReactJS developer, you harmoniously blend components and styles to create an eye-catching UI. It might seem overwhelming at first, but with practice, you'll find that styling in ReactJS is not just a task, but an art. So, go ahead and start your journey of transforming your application from a blank canvas to a masterpiece. Happy styling!