How to Get Theme from Store and Switch the App Theme On react-admin?

Implement light/dark theme switching in react-admin by integrating Redux state, custom MUI themes, and a reusable toggle button in your layout.

Summarize with AI

Not enough time? get the key points instantly.

If you want to dynamically switch the App Theme in a React-Admin app using Redux or another global store, you must integrate a custom theme provider with React-Admin’s component and sync it with your state logic.

Step‑by‑Step Guide

Implement a seamless light/dark theme toggle in your React-Admin app using Material UI, Redux, and a custom UI control step by step.

Step 1: Define Your Themes

Create reusable MUI theme objects to represent the light and dark versions of your app.

// themes.jsimport { createTheme } from “@mui/material/styles”;

 

export const lightTheme = createTheme({

  palette: {

    mode: “light”,

    primary: { main: “#1976d2” },

  },

});

 

export const darkTheme = createTheme({

  palette: {

    mode: “dark”,

    primary: { main: “#90caf9” },

  },

});

Step 2: Add Theme Slice to Your Store

Create a slice to store and toggle the current theme mode (light or dark).

// themeSlice.js (Redux example)import { createSlice } from “@reduxjs/toolkit”;

 

const initialState = {

  mode: “light”, // or “dark”

};

 

const themeSlice = createSlice({

  name: “theme”,

  initialState,

  reducers: {

    toggleTheme: (state) => {

      state.mode = state.mode === “light” ? “dark” : “light”;

    },

  },

});

 

export const { toggleTheme } = themeSlice.actions;

export default themeSlice.reducer;

Step 3: Connect Admin to Store Theme

Fetch the current theme mode from the Redux store and pass the matching MUI theme to React-Admin.

// App.tsximport { Admin } from “react-admin”;

import { useSelector } from “react-redux”;

import { lightTheme, darkTheme } from “./themes”;

 

const App = () => {

  const themeMode = useSelector((state) => state.theme.mode);

  const theme = themeMode === “dark” ? darkTheme : lightTheme;

 

  return ;

};

 

export default App;

Step 4: Add a Theme Toggle UI

Create a UI control (e.g., icon button) that allows users to switch between light and dark themes by dispatching the toggle action.

// ThemeToggle.tsximport { useDispatch, useSelector } from “react-redux”;

import { toggleTheme } from “./themeSlice”;

import IconButton from “@mui/material/IconButton”;

import Brightness4Icon from “@mui/icons-material/Brightness4”;

 

const ThemeToggle = () => {

  const dispatch = useDispatch();

  const mode = useSelector((state) => state.theme.mode);

 

  return (

     dispatch(toggleTheme())} color=”inherit”>

      

    

  );

};

 

export default ThemeToggle;

Step 5: Place Toggle Button in Layout

Insert the toggle button into your app bar, sidebar, or a persistent header so it’s accessible across views.

// Example: Inside custom AppBarimport { AppBar } from “react-admin”;

import ThemeToggle from “./ThemeToggle”;

const MyAppBar = (props) => (

  

    

  

);


Resources

Insights from our engineering leaders.

Our team shares practical insights on the technologies and engineering decisions shaping enterprise software today, grounded in real client work.

ReactJS

Filter Items Based On Multiple Properties

Why am I getting this problem? If you apply separate filters in sequence or only check one property at a time your code will match…