DEV Community

Elightwalk Technology
Elightwalk Technology

Posted on

Styling in React: CSS-in-JS vs. Traditional CSS

Styling in React can be approached in various ways, including using traditional CSS and modern CSS-in-JS libraries. Each method has advantages and disadvantages, and choosing the right one depends on your project's needs.

Overview of CSS-in-JS Libraries
CSS-in-JS libraries allow you to write CSS directly within your JavaScript code. Some popular libraries include:

Styled components: Utilizes tagged template literals to style components.

import styled from 'styled-components';

const Button = styled.button`
  background: blue;
  color: white;
  padding: 10px;
`;
Enter fullscreen mode Exit fullscreen mode

Emotion: Offers both CSS-in-JS and the styled API similar to styled components.

/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';

const buttonStyle = css`
  background: blue;
  color: white;
  padding: 10px;
`;

function Button() {
  return <button css={buttonStyle}>Click me</button>;
}
Enter fullscreen mode Exit fullscreen mode

JSS: JavaScript-based styling solution that allows you to define styles as JavaScript objects.

import { createUseStyles } from 'react-jss';

const useStyles = createUseStyles({
  button: {
    background: 'blue',
    color: 'white',
    padding: '10px'
  }
});

function Button() {
  const classes = useStyles();
  return <button className={classes.button}>Click me</button>;
}
Enter fullscreen mode Exit fullscreen mode

Benefits and Drawbacks of CSS-in-JS

Benefits
Scoped Styles: Styles are scoped to components, reducing the risk of global namespace collisions.

Dynamic Styling: Styles can be based on component props, making creating dynamic and conditional styles easy.

Better Maintenance: Co-locating styles with components can make maintaining and understanding the codebase easier.

Theming: Easier to implement and manage themes across the application.

Drawbacks
**Performance: **Sometimes, runtime style generation can be slower than precompiled CSS.

Learning Curve: New syntax and concepts may have a learning curve for developers used to traditional CSS.

Tooling: This may require additional configuration for server-side rendering and integration with existing tools.

Integrating Traditional CSS and CSS Modules with React

Traditional CSS

You can import traditional CSS files directly into your components or main entry files

import './App.css';

function App() {
  return <div className="app">Hello World</div>;
}
Enter fullscreen mode Exit fullscreen mode

CSS Modules
CSS Modules provide locally scoped CSS by default, avoiding global namespace collisions.

/* App.module.css */
.app {
  background: blue;
  color: white;
}
Enter fullscreen mode Exit fullscreen mode
import styles from './App.module.css';

function App() {
  return <div className={styles.app}>Hello World</div>;
}
Enter fullscreen mode Exit fullscreen mode

Best Practices for Theming and Design Systems

Centralized Theme: Define a theme object with colors, fonts, and other design tokens.

const theme = {
  colors: {
    primary: 'blue',
    secondary: 'green'
  },
  spacing: {
    small: '8px',
    medium: '16px',
    large: '24px'
  }
};
Enter fullscreen mode Exit fullscreen mode

Theming with CSS-in-JS: Use a ThemeProvider to pass the theme to styled-components or Emotion.

import { ThemeProvider } from 'styled-components';

function App() {
  return (
    <ThemeProvider theme={theme}>
      <MyComponent />
    </ThemeProvider>
  );
}
Enter fullscreen mode Exit fullscreen mode

Consistent Design Language: Use design tokens and utility classes to ensure component consistency.

Performance Considerations and Best Practices

Minimize Re-renders: Ensure that your CSS-in-JS library doesn't cause unnecessary re-renders.

Server-Side Rendering: Use libraries that support server-side rendering to improve initial load times and SEO.

Code Splitting: Use code-splitting to load only the styles required for the current view, reducing initial load times.

Static Extraction: Some CSS-in-JS libraries offer static extraction to compile styles at build time, improving runtime performance.

Conclusion

Current React development makes use of both traditional CSS and CSS-in-JS. Traditional CSS and CSS modules provide familiarity and simplicity, while CSS-in-JS offers scoped and dynamic styling capabilities. Selecting the best strategy will depend on the particular needs of your project, such as the requirement for dynamic styles, performance considerations, and developer experience. To get the best ReactJS development service, you have to check the basics of modern styling skills with a react developers.

Top comments (0)