React-JSS
JSS is a CSS-in-JS library that allows developers to author styles using JavaScript. `react-jss` provides a robust integration layer for using JSS within React applications, leveraging hooks (like `createUseStyles`) and a `ThemeProvider` for contextual styling. It enables dynamic styling, global styles, and efficient style injection into the DOM. The current stable version is 10.10.0, though the project is explicitly stated as no longer maintained as of v10.10.1. This means new features, critical bug fixes, or compatibility updates for newer React versions (beyond what was already addressed) are not expected. Historically, it offered a performant and feature-rich alternative to other CSS-in-JS solutions, supporting various plugins for advanced styling patterns like nesting and global rules. Due to its abandonment, it is generally not recommended for new projects and existing projects should consider migration. Its release cadence was irregular towards its end, with several alpha and patch releases addressing React 18 compatibility and bug fixes.
Common errors
-
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause `createUseStyles` is a React Hook and must be called within a functional component or another custom hook, not in a class component or outside React's render phase.fixEnsure `createUseStyles` is called directly inside a functional component's body or within a custom hook that respects React's Rules of Hooks. -
TypeError: Cannot read properties of undefined (reading 'theme')
cause Attempting to access `theme` properties within a style definition or component without a `ThemeProvider` being present higher up in the component tree.fixWrap your component tree (or the relevant section) with `<ThemeProvider theme={yourThemeObject}>` to provide the theme context. -
Module not found: Can't resolve 'react-jss'
cause The `react-jss` package is not installed or the import path is incorrect.fixRun `npm install react-jss` or `yarn add react-jss` to install the package. Verify the import statement is `import { ... } from 'react-jss';`.
Warnings
- breaking The `react-jss` project is officially no longer maintained. No new features, bug fixes, or compatibility updates for future React versions are expected. Users should consider migrating to alternative styling solutions.
- gotcha There were several bug fixes and reverts related to React 18 compatibility, specifically concerning `useInsertionEffect`. While patched in v10.9.2, ensure thorough testing when using `react-jss` with React 18 or newer, as future React updates may reintroduce issues due to lack of maintenance.
- gotcha Memory leaks were reported and addressed in specific JSS plugins (e.g., `jss-plugin-global`, `jss-plugin-nested`, `jss-plugin-rule-value-function`) with subsequent reverts in some cases. This indicates potential instability around memory management in complex styling scenarios.
Install
-
npm install react-jss -
yarn add react-jss -
pnpm add react-jss
Imports
- createUseStyles
const useStyles = require('react-jss').createUseStyles;import { createUseStyles } from 'react-jss'; - ThemeProvider
import ThemeProvider from 'react-jss/lib/ThemeProvider';
import { ThemeProvider } from 'react-jss'; - JssProvider
import { JssProvider } from 'react-jss';
Quickstart
import React from 'react';
import { createUseStyles, ThemeProvider } from 'react-jss';
interface Theme {
primary: string;
secondary: string;
fontSize: number;
}
const theme: Theme = {
primary: '#007bff',
secondary: '#6c757d',
fontSize: 16,
};
const useStyles = createUseStyles<keyof Theme> ({
button: {
backgroundColor: ({ primary }) => primary,
color: 'white',
padding: '10px 20px',
border: 'none',
borderRadius: '5px',
fontSize: ({ fontSize }) => `${fontSize}px`,
cursor: 'pointer',
'&:hover': {
opacity: 0.9,
},
},
secondaryButton: {
backgroundColor: ({ secondary }) => secondary,
color: 'white',
padding: '10px 20px',
border: 'none',
borderRadius: '5px',
fontSize: ({ fontSize }) => `${fontSize}px`,
cursor: 'pointer',
marginLeft: '10px',
'&:hover': {
opacity: 0.9,
},
},
});
function MyButton() {
const classes = useStyles(theme); // styles can receive props or use the theme from context
return (
<div>
<button className={classes.button}>Primary Button</button>
<button className={classes.secondaryButton}>Secondary Button</button>
</div>
);
}
function App() {
return (
<ThemeProvider theme={theme}>
<MyButton />
</ThemeProvider>
);
}
export default App;