mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.38k stars 32.14k forks source link

Nivo Charts wont animate if it is inside a styled() MUI component #35066

Closed KissBalazs closed 1 year ago

KissBalazs commented 1 year ago

Duplicates

Latest version

Steps to reproduce 🕹

Link to live example: https://stackblitz.com/edit/react-ts-qm2e32?file=App.tsx

Current behavior 😯

Nivo charts won't animate and noticeable flickers upon data update inside a styled MUI component.

Expected behavior 🤔

To be able to animate the Nivo chart inside a styled MUI component.

Context 🔦

I understand that Nivo chart is a different library, but this felt like the roots of the issue is in MUI. As far as I know, I am using the tool as it is supposed to be, but upon wrapping it with a mui styled() component, it breaks the tool.

Your environment 🌎

see stackblitz example

sai6855 commented 1 year ago

hey @KissBalazs the problem is you have declared StyledTable inside App function, so every time state changes inside App new StyledTable function is created. Since new StyledTable is created everytime, react unmounts and remounts children inside StyledTable, because of this unmount->remount->unmount behavoiur you re seening flickering behaviour.

To fix this just create StyledTable outside App.

import './style.css';

const StyledTable = styled(Table)(({ theme }) => ({
  background: 'gray',
}));

export default function App() {
  const [testData, setTestData] = React.useState<any>(50);

  function generateNewdata() {
    setTestData(Math.floor(Math.random() * 100));
  }

working example: https://stackblitz.com/edit/react-ts-jrzrc2?file=App.tsx

KissBalazs commented 1 year ago

Thank you @sai6855 so much for the clear answer. I learned something new again about React!