-
-
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
[docs] withTheme
is missing
#27199
Comments
Thanks for the report! The documentation should be updated. It should be index 073583e080..ab1d0efb56 100644
--- a/docs/src/pages/styles/advanced/advanced.md
+++ b/docs/src/pages/styles/advanced/advanced.md
@@ -51,7 +51,7 @@ function DeepChild() {
For use in class or function components:
```jsx
-import { withTheme } from '@material-ui/core/styles';
+import { withTheme } from '@material-ui/styles';
function DeepChildRaw(props) {
return <span>{`spacing ${props.theme.spacing}`}</span>; Note that any documentation inside https://next.material-ui.com/styles/api is related to the legacy JSS styling package Regarding the |
Even if this is trivial, it would be nice not to have to add the same code to every project that uses MUI. Unfortunately, the Actually, I found implementing this in TypeScript to be surprisingly difficult. Trying to remove the import React, { FunctionComponent, ReactElement } from 'react';
import { Theme, useTheme } from '@material-ui/core/styles';
type WithoutTheme<TProps> = Omit<TProps, 'theme'>;
const withTheme = function<TProps extends { theme: Theme }> (Component: FunctionComponent<TProps>): FunctionComponent<WithoutTheme<TProps>> {
return (props: WithoutTheme<TProps>): ReactElement => {
const theme = useTheme();
return (
<Component theme={ theme } { ...props } />
);
};
};
export {
withTheme
}; This results in the following error:
The solution is to use import { useTheme } from '@material-ui/core/styles';
import React, { FunctionComponent, ReactElement } from 'react';
type WithoutTheme<TProps> = Omit<TProps, 'theme'>;
const withTheme = function<TProps extends { theme: Theme }> (Component: FunctionComponent<TProps>): FunctionComponent<WithoutTheme<TProps>> {
return (props: any): ReactElement => {
const theme = useTheme();
return (
<Component theme={ theme } { ...props } />
);
};
};
export {
withTheme
}; Adding this function back in would save a lot of people a lot of headaches. The state of styled-components support in MUI5 does not feel very cohesive at the moment and not having familiar helpers like this one adds to that. |
Maybe I don't understand the use case behind using styled-components with MUI. As it is at the moment, I can't see how it is useful. On one hand, we can't use On the other hand, when we use I suspect that using styled-components alongside MUI is not really how MUI should be used, seeing how it has a CSS API and |
You can use
Can you explain what do you mean by "sufficiently different"? Again, you can still use
You can use
Can you share some pain points? We are working on a codemod for the v5 migration. Any feedback is more than welcomed. @strangedev would be great if you can open a dedicated issue about the pain points so that we can discussed then in a more structured manner. |
Thanks for your answer @mnajdova, the advice is very much appreciated 😊 It seems we were not fully aware of how to integrate v5. Might be that the behavior we're experiencing is related to the aliasing with webpack not working as expected in Next.js. I will test it again with a smaller codebase and get back to you in a separate issue if we're still experiencing problems. |
@strangedev On the same topic: #27087. Do you have a specific reason for using |
None that couldn't ultimately be replaced with |
@oliviertassinari what do you think if we bring
|
@siriwatknp I think that we can wait for pushbacks from the community. So far, this issue seems to be about the wrong link in the documentation. If we can make it harder for developers to use class components, we might even argue that it's healthy. |
Current Behavior 😯
The v5 documentation shows the
withTheme
function which injects theTheme
as a prop into a component. The function is not available in@material-ui/core/styles
as per the documentation.@material-ui/styled-engine
exportswithTheme
, but it seems to be a type only and not the actual function.Expected Behavior 🤔
I should either be able to use the
withTheme
function, or the docs should be updated.Steps to Reproduce 🕹
I've implemented a small example in a next.js application: https://github.com/strangedev/mui5-sc-bugs/blob/with-theme-missing/src/components/WithTheme.tsx
When trying to run with
npm run dev
, you can see thatwithTheme
from@material-ui/styled-engine
is not a function.Try changing the import to any of the mentioned modules to show that the function is indeed missing.
Context 🔦
I'm migrating from v5 alpha with styled-components as an additional styling solution to v5 beta with styled-components as styled-engine for MUI.
Your Environment 🌎
`npx @material-ui/envinfo`
The text was updated successfully, but these errors were encountered: