-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
makeStyles, withTheme and useTheme not working #14416
Comments
@ericsolberg This probably broke in #13229. Be sure to follow https://material-ui.com/css-in-js/basics/#migration-for-material-ui-core-users in your own code. |
@ericsolberg This is exactly the type of frustration that working on v4 enable us to fix. I can think of the following possible approaches:
|
@oliviertassinari It broke the dependencies in the codesandbox for hooks version. This shouldn't be an issue once stable react with hooks is released (which should've been yesterday). |
Oh, so there are multiple problems :). |
would this mean |
@ericsolberg I'm new to both ts and mui so don't really know what I'm doing, but what worked for me was:
(using createMuiTheme and Theme from @material-ui/core/styles and all else from @material-ui/styles) |
I have a similar issue and I'm not sure to understand what I did wrong. Before
After
Before
After
|
@VincentLanglet Are you performing the bootstrap step? https://material-ui.com/css-in-js/basics/#migration-for-material-ui-core-users |
Hey folks, thanks for your work on this project. I am having the exact same issue. With ThemeProvider the theme doesn't seem to be applied. I am definitely doing the bootstrap step. With MuiThemeProvider the theme is applied but the makeStyles function does not get the Theme object. makeStyles does get it with ThemeProvider. |
@mrstafford makeStyles is part of @material-ui/styles, therefore it is only intended to work with the ThemeProvider. |
@joshwooding yep :). I was explaining my situation, I should have been more clear. I think the specific problem is with ThemeProvider not applying the theme whereas if I use MuiThemeProvider it works fine. This is with the following dependencies: The code is pretty simple: <StylesProvider jss={myJss} generateClassName={generateClassName}>
<ThemeProvider theme={myTheme}>
<CssBaseline />
<MyPage />
</ThemeProvider>
</StylesProvider> |
@mrstafford Do you have a reproduction I can have a look at? |
@joshwooding I believe the following codesandbox displays the issue. If you change ThemeProvider to MuiThemeProvider it will change the background color to dark. https://codesandbox.io/s/q9lmro76y4?fontsize=14 Actually. Really strangely it was showing the issue when I created it. Now it is working. I'll try to see the difference between it and my project. |
@mrstafford When I view your codesandbox I get the dark background straight away (using the ThemeProvider) |
@joshwooding you are right. When I remove, save and then re-add import "./bootstrap"; it breaks again. |
@joshwooding figured it out. the import "./bootstrap"; must be the very top import of the index.js. i.e. import "./bootstrap";
import React from "react";
import ReactDOM from "react-dom";
import App from "./app"; instead of import React from "react";
import ReactDOM from "react-dom";
import App from "./app";
import "./bootstrap"; Thank you for your help. |
@mrstafford Your bootstrap is being imported after your App, it should be before Looks like you beat me to it :) import React from "react";
import ReactDOM from "react-dom";
import "./bootstrap";
import App from "./app"; will work as well |
it is a human error of appreciation
import { ThemeProvider } from '@material-ui/styles';
error is related to a typeof but sounds like a package error: Attempted import error: 'ThemeProvider' is not exported from '@material-ui/core/styles'. |
You can use imports "like this". Just import the correct names (ThemeProvider -> MuiThemeProvider). |
I just wasted a few hours on figuring out why my theme wasn't being applied... hopefully this comment helps someone: This compiles and auto-completes in my IDE using typescript, but definitely does not work:
It should just be:
|
@lookfirst What's your IDE? Do you have a way to configure it, to get the correct import? |
IDEA. Sure, you can pick the right import from the list, but I picked the wrong one because I've been badly trained in the past to pull the most 'complete' import so that tree shaking would work better. Yes, I know that is moot these days, but whatever... Would be nice if MUI had a more restricted set of exports so that stuff like this didn't get picked up by accident. |
@lookfirst I can't find any way to configure the auto-import from our side. Tree-shaking should work with the latest version of Material-UI. You should also be able to leverage https://material-ui.com/guides/minimizing-bundle-size/#option-2 for better perf in dev mode. |
Control the exports. Google Firebase client (not admin) SDK has some crazy crazy stuff going on in that department if you want to look into it. Since option 2 is the 'best UX/DX', shouldn't it be noted as option 1? =) Also, I'm using CRA, so updating the instructions to make sure that usecase is covered seems like a wise option given the ubiquity of that solution. That said, from what I can tell, it just magically works. But you're more of an expert than I on this matter... so best to double check. |
@lookfirst It does work, but its 10-30% slower to start your dev server. Yeah CRA is a challenge. It requires the usage of another dependency. I can't find any material on the auto import work Firebase has done. Do you have a link to share? |
@oliviertassinari In the above link, you mention: "Pick one of the following plugins:" for the babel option two. Just some feedback, it would be nice to know the differences between those two options. Why should I pick one plugin over the other? The confusing morass of TS / JS / Babel / CRA / etc... makes it an endless rabbit hole and any little extra bit of information there helps a lot. Thanks! Here is the info on firebase: https://firebase.google.com/docs/web/setup Then try clicking through to see the tricks they employ with typescript/npm/package.json. |
It seems that they have inlined everything at the root: https://unpkg.com/browse/firebase@6.3.5/index.d.ts. |
@oliviertassinari Ah yes, but dig a little deeper: https://unpkg.com/browse/firebase@6.3.5/app/package.json They push all the 'code' into |
Thanks for the details. It's an interesting approach. We should consider having a flat file for each folder, maybe it's actually better than what we do now. |
I'm trying to use the sample code for an app bar provided in the documentation (note that I'm not using the AppBar component- I have my own header component. But I'm currently trying to create a SearchField component based on this code). I've basically cut and pasted the code as-is, and pulled out the parts not related to the search field. I'm getting an error:
Uncaught TypeError: Cannot read property 'borderRadius' of undefined
. Digging into it, I see that the theme object being passed into the function I pass into makeStyles is an empty object. I investigated further, to see if I could get my hands on the theme. When I export my component with withTheme()(searchBar), the theme object in the props is null. Likewise, if I just call useTheme() in my component, it also returns null.Note: it seems the plain JS sample code (no hooks) works - so this has something to do with the Material-UI and React 16.8.0-alpha.1. However- this version is required to use the sample hooks code.
Expected Behavior 🤔
My makeStyles function should be passed the populated theme object, and withTheme and useTheme should return the proper non-null theme object.
Current Behavior 😯
The theme object passed into makeStyles is empty. The theme props attribute injected by withTheme is null. The theme object returned by useTheme is null.
Steps to Reproduce 🕹
Link: I don't have a live link but it's easy to reproduce:
Context 🔦
I'm trying to implement a toolbar in my app with Material-UI.
Your Environment 🌎
The text was updated successfully, but these errors were encountered: