Skip to content

Updating an atom unmounts and mounts component inside the Styled component #2606

Closed Answered by rothsandro
GoaKoala asked this question in Bug report
Discussion options

You must be logged in to vote

You create the styled component AppBarStyled inside another React component but they should always be created outside of the React component (or the render method in case of class components). Your current solution creates a new React component on every render, that's why all child components are destroyed and re-created (unmounted + mounted). It's not related to Jotai but can be reproduced with useState() as well.

Just create the styled component outside:

const StyledComponent = () => {
  // WRONG 
  // const AppBarStyled = styled(AppBar)(({ theme }) => ({ }));

  return (
    <AppBarStyled>
      <StyledComponentContent></StyledComponentContent>
    </AppBarStyled>
  );
};

// CORRECT
c…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@GoaKoala
Comment options

Answer selected by GoaKoala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants