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

handle styled(Component)(object/function) syntax #179

Open
jsardev opened this issue Nov 16, 2018 · 3 comments
Open

handle styled(Component)(object/function) syntax #179

jsardev opened this issue Nov 16, 2018 · 3 comments

Comments

@jsardev
Copy link

jsardev commented Nov 16, 2018

I'm using styled-components with SSR heavily and found few cases when the server-rendered markup mismatches on the client-side, resulting in a console warning in development mode.

After a lot of digging, I noticed that the problem goes away when I resign from the object/function syntax for extended components, like this:

styled(Component)(props => ({ color: props.color }));
styled(Component)({ color: 'red' });

This happens because this babel plugin does not ensure that styled-components with this kind of syntax will result in consistent component-id while doing SSR.

I tried to provide a PR by myself but I don't have much experience in compiler stuff so it took me a lot of time to start to understand anything in this repository 😥 And unfortunately I don't have more time to sit on this. But I managed to set up a failing test for this case - I guess it might be helpful.

test/fixtures/add-identifier/index.js

const WrappedComponentObjectForm = styled(Inner)({ color: red })
const WrappedComponentFunctionForm = styled(Inner)(p => ({ color: p.color || 'red' }))

test/snapshots/index.test.js.snap

const WrappedComponentObjectForm = styled(Inner).withConfig({
  componentId: \\"wkc8iz-6\\"
})({
  color: red
});
const WrappedComponentFunctionForm = styled(Inner).withConfig({
  componentId: \\"wkc8iz-7\\"
})(p => ({
  color: p.color || 'red'
}));"

I think handling this case could resolve few issues like #147 or next.js#3706.

I would very appreciate any action on this issue as I'm completely out of time right now and I'm sure someone with greater experience with AST's or particularly this repository could do this without spending a lot of time on it.

@KyorCode
Copy link

I'm running into this problem in 2020, was this every fixed?
When wrapping an external library component with styled(Component), class mismatches are generated both in develop and production mode. I have tried every possible solution in the related issues, but can not seem to get it to work.

@mario-jerkovic
Copy link

Same here. I don't get this issue in development mode, only in production.

@mnajdova
Copy link

Same issue is surfacing when using mui v5 with styled-components. I am not sure if it is also related to it being an external dependency (there is another issue around this #358)

This is the original issue posted on MUI - mui/material-ui#29742

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants