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 · 11 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

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

4 participants