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

Ternary sx statement with undefined causes component unmount/remount #2164

Open
jonathanharrell opened this issue Mar 7, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@jonathanharrell
Copy link

Describe the bug
Let's say I have a basic React component:

const Test = (props) => {
  return (
    <div {...props}>{props.children}</div>
  );
};

And then I use sx on that component, swapping between an object and undefined based on some state:

<Test sx={someBoolean ? myStyles : undefined}/>

This causes the Test component to unmount/remount (along with any children it renders).

This is because when the sx prop is defined, Test is rendered inside EmotionCssPropInternal and when it's undefined, it's rendered on its own.

I don't believe this is the case when using emotion's css prop.

Expected behavior
EmotionCssPropInternal should wrap any component that uses sx prop, even if that prop value is undefined.

@lachlanjc
Copy link
Member

Appreciate the report! Is this causing issues in your project? Or more like a "oh that's funky" that's not meaningfully slowing it down?

@hasparus
Copy link
Member

I had been initially conflicted about handling sx prop set to undefined differently than missing sx prop.
However, I understand this remount can cause component state to be discarded resulting in a unexpected gotcha moment.

For now, could you an empty object {} instead of undefined?

@hasparus hasparus added the bug Something isn't working label Mar 11, 2022
@jonathanharrell
Copy link
Author

Yes, I am using {} for now to resolve the issue. It was an unexpected behavior and took a while for me to track down what was causing component remounts, so I think at the very least it should be documented. Thanks for the response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants