Skip to content
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

Updating from v5 to v6 doesnt work: createStyled.js:26 Uncaught TypeError: Cannot assign to read only property 'theme' of object '#<Object>' #44343

Closed
BennyAlex opened this issue Nov 7, 2024 · 21 comments
Assignees
Labels
package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information

Comments

@BennyAlex
Copy link

BennyAlex commented Nov 7, 2024

Steps to reproduce

image

I am trying to upgrade to v6 and also used the codemods but I am seeing this error.

Current behavior

No response

Expected behavior

No response

Context

No response

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: 'theme' of object, reateStyled.js:26

@BennyAlex BennyAlex added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 7, 2024
@BennyAlex
Copy link
Author

I think the error came from using a styled component and passing the theme to it

and the theme was imported from the theme file (createTheme({})

@DiegoAndai DiegoAndai self-assigned this Nov 7, 2024
@DiegoAndai DiegoAndai moved this to Backlog in Material UI Nov 7, 2024
@DiegoAndai DiegoAndai added the package: material-ui Specific to @mui/material label Nov 7, 2024
@DiegoAndai
Copy link
Member

Hey @BennyAlex, thanks for the report! Could you provide a minimal reproduction? This would help a lot. A repo sharing your project would be perfect. Thank you!

@DiegoAndai DiegoAndai added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 7, 2024
@DiegoAndai DiegoAndai moved this from Backlog to Waiting for response in Material UI Nov 11, 2024
@visualjeff
Copy link

I have the same issue here with a component. Exact same error message after upgrading to v6.

npx @mui/envinfo

  System:
    OS: macOS 13.5
  Binaries:
    Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v18.19.1/bin/npm
    pnpm: 8.14.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 130.0.6723.117
    Edge: Not Found
    Safari: 16.6
  npmPackages:
    @emotion/react:  11.13.3 
    @emotion/styled:  11.13.0 
    @mui/base:  5.0.0-beta.61 
    @mui/core-downloads-tracker:  6.1.7 
    @mui/lab: 6.0.0-beta.14 => 6.0.0-beta.14 
    @mui/material:  6.1.6 
    @mui/private-theming:  6.1.7 
    @mui/styled-engine:  6.1.7 
    @mui/system:  6.1.6 
    @mui/types:  7.2.19 
    @mui/utils:  6.1.6 
    @mui/x-date-pickers: 7.22.2 => 7.22.2 
    @mui/x-internals:  7.21.0 
    @mui/x-tree-view:  7.22.1 
    @types/react:  18.3.12 
    react: 18.3.0-canary-2c338b16f-20231116 => 18.3.0-canary-2c338b16f-20231116 
    react-dom: 18.3.0-canary-2c338b16f-20231116 => 18.3.0-canary-2c338b16f-20231116 
    typescript: 5.3.3 => 5.3.3 

Is there any upgrade guidance for who use styled? Any tools out there that can validate our stylings?

@DiegoAndai
Copy link
Member

Hey @visualjeff, thanks for the report! Could you provide a minimal reproduction? This would help a lot. A live example would be perfect. A repo or a StackBlitz sandbox would work. Thank you!

@visualjeff
Copy link

visualjeff commented Nov 13, 2024

I think I figured out our issue while creating repro code. Don't pass the theme down as a prop so you can use it to style a component.

const MarkdownText = ({
    textType = 'disclaimer',
    markdownText = 'test 123',
    uniqueId = '123',
    enableFontWeight = true
}: MarkdownTextPropType) => {
    const theme = useTheme();

    return (
        <StyledMarkdownTitleText
            enableFontWeight={enableFontWeight}
            textType={textType}
            theme={theme}
            uniqueId={uniqueId}
        >
            <MarkdownRenderer markdown={markdownText} />
        </StyledMarkdownTitleText>
    );
};

@BennyAlex
Copy link
Author

I think I figured out our issue while creating repro code. Don't pass the theme down as a prop so you can use it to style a component.

const MarkdownText = ({
    textType = 'disclaimer',
    markdownText = 'test 123',
    uniqueId = '123',
    enableFontWeight = true
}: MarkdownTextPropType) => {
    const theme = useTheme();

    return (
        <StyledMarkdownTitleText
            enableFontWeight={enableFontWeight}
            textType={textType}
            theme={theme}
            uniqueId={uniqueId}
        >
            <MarkdownRenderer markdown={markdownText} />
        </StyledMarkdownTitleText>
    );
};

thats what I also wrote here

I think the error came from using a styled component and passing the theme to it

and the theme was imported from the theme file (createTheme({})

so thats basically the solution to remove the theme prop from styled components

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 14, 2024
@visualjeff
Copy link

So I think we can close the issue.

@DiegoAndai
Copy link
Member

@BennyAlex so I understand you were able to solve the issue on your side as well?

@DiegoAndai DiegoAndai added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 15, 2024
Copy link

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

@github-project-automation github-project-automation bot moved this from Waiting for response to Done in Material UI Nov 23, 2024
@MosheHM
Copy link

MosheHM commented Nov 24, 2024

i just ending the migration from v4 to v6 and faced the same issue but it didn't come from const theme = createTheme(); and i still can't find the cause of this error

@MosheHM
Copy link

MosheHM commented Nov 24, 2024

I think the error came from using a styled component and passing the theme to it

and the theme was imported from the theme file (createTheme({})

and i think the reason is like @BennyAlex said when you trying to insert the theme and when you take the global theme and use it inside the styled component the scop theme is trying to override the global theme and the global theme is a frozen object and then you get this error TypeError
image

@BennyAlex
Copy link
Author

I think the error came from using a styled component and passing the theme to it
and the theme was imported from the theme file (createTheme({})

and i think the reason is like @BennyAlex said when you trying to insert the theme and when you take the global theme and use it inside the styled component the scop theme is trying to override the global theme and the global theme is a frozen object and then you get this error TypeError image

Not sure if withTheme is still needed or even supported. Have you tried without it?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 26, 2024
@github-actions github-actions bot reopened this Nov 26, 2024
@tianyingchun
Copy link

face the same issue too.

⨯ ../../node_modules/@mui/system/esm/createStyled/createStyled.js (26:1) @ attachTheme
⨯ Internal error: TypeError: Cannot assign to read only property 'theme' of object '#'
at attachTheme (../../node_modules/@mui/system/esm/createStyled/createStyled.js:39:15)

@tianyingchun
Copy link

it seems that we should not pass
const theme = useTheme() as props for IconButtonStyle

const IconButtonStyle = styled(MuiIconButton, {
  shouldForwardProp: (prop) => prop !== 'variant' && prop !== 'shape',
})(({ theme, variant, shape, color }: StyleProps) => ({
  position: 'relative',
  '::after': {
  

@MosheHM
Copy link

MosheHM commented Nov 26, 2024

I think the error came from using a styled component and passing the theme to it
and the theme was imported from the theme file (createTheme({})

and i think the reason is like @BennyAlex said when you trying to insert the theme and when you take the global theme and use it inside the styled component the scop theme is trying to override the global theme and the global theme is a frozen object and then you get this error TypeError image

Not sure if withTheme is still needed or even supported. Have you tried without it?

you are right the withTheme is not needed but it's still in supported this code is from the prevues version and the codemod to v6 isn't replace it then i deleted it and every thing ok

@MosheHM
Copy link

MosheHM commented Nov 26, 2024

it seems that we should not pass const theme = useTheme() as props for IconButtonStyle

const IconButtonStyle = styled(MuiIconButton, {
  shouldForwardProp: (prop) => prop !== 'variant' && prop !== 'shape',
})(({ theme, variant, shape, color }: StyleProps) => ({
  position: 'relative',
  '::after': {
  

yes as you can see this is the same,
just in the new version we can't use the global Theme inside a styled component

@DiegoAndai
Copy link
Member

Hey @MosheHM, from this: "then i deleted it and every thing ok" I understand you were able to solve your issue?

@tianyingchun seems like your case is the same and can also be solved, as you mention: #44343 (comment)

@DiegoAndai DiegoAndai added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 27, 2024
@tianyingchun
Copy link

tianyingchun commented Nov 28, 2024 via email

@MosheHM
Copy link

MosheHM commented Nov 28, 2024

Hey @MosheHM, from this: "then i deleted it and every thing ok" I understand you were able to solve your issue?

@tianyingchun seems like your case is the same and can also be solved, as you mention: #44343 (comment)

Hi @DiegoAndai, indeed that solve the problem

@DiegoAndai
Copy link
Member

Closing this as we have found the solution. If you're dealing with the same problem, please checkout this comment.

Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@BennyAlex How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information
Projects
Status: Done
Development

No branches or pull requests

5 participants