All you have to do is to import and use createMuiTheme instead of using the theme directly and you can export the plain objects from dakTheme.js and lightTheme.js
you have to recreate theme each time the app stat changes (user toggles theme)
to avoid unnecessary reRenders of the ThemeProvider and as a result reRendering of the whole app you should use a useMemo react hook for changing the theme depending on the dark state.
App.js
import React, { useState, useMemo } from "react";
import { ThemeProvider, CssBaseline } from "@material-ui/core";
import darkTheme from "./Theme/darkTheme";
import lightTheme from "./Theme/lightTheme";
import PrimarySearchAppBar from "./Components/Appbar";
import { createMuiTheme } from "@material-ui/core";
function App() {
const [dark, setDark] = useState(true);
const Theme = useMemo(() => createMuiTheme(dark ? darkTheme : lightTheme), [dark]);
return (
<ThemeProvider theme={Theme}>
<CssBaseline />
<div>
<PrimarySearchAppBar thm={dark} Togglethm={setDark} />
</div>
</ThemeProvider>
);
}
export default App;
dartkTheme.js
const darkTheme = {
palette: {
type: "dark",
primary: {
main: "#00e676",
},
secondary: {
main: "#e6006f",
}
},
}
export default darkTheme;
lightTheme.js
const lightTheme = {
palette: {
type: "light",
primary: {
main: "#2196f3"
},
secondary: {
main: "#f50057"
}
}
};
export default lightTheme;