Skip to content

Commit

Permalink
🐛Snackbar: fix setTimeout running every render (#3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddvernes authored Aug 20, 2024
1 parent b374b12 commit 9b3148a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,42 @@ export const WithAction: StoryFn<SnackbarProps> = () => {
)
}
WithAction.storyName = 'With action'

export const TimerRegressionTest: StoryFn<SnackbarProps> = () => {
const [message, setMessage] = useState({ open: false, text: '' })
const [data, setData] = useState(0)

const handleClick = () => {
setMessage({ open: !message.open, text: 'Hello, World!' })
}

const handleDataClick = () => {
setData(data + 1)
}

return (
<>
<div>
<Button onClick={() => handleClick()}>Show Snackbar</Button>
<Button
style={{ marginLeft: '10px' }}
onClick={() => handleDataClick()}
>
Data + {data}
</Button>
</div>

<Snackbar
open={message.open}
onClose={() => setMessage({ open: false, text: '' })}
autoHideDuration={2000}
>
{message.text}
</Snackbar>
</>
)
}
TimerRegressionTest.storyName =
'Test to ensure timer stays consistent across rerenders'
//hide this story from sidebar
TimerRegressionTest.tags = ['!dev']
3 changes: 2 additions & 1 deletion packages/eds-core-react/src/components/Snackbar/Snackbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export const Snackbar = forwardRef<HTMLDivElement, SnackbarProps>(
}, autoHideDuration)
}
return () => clearTimeout(timer.current)
}, [open, setVisible, autoHideDuration, onClose])
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, autoHideDuration])

const props = {
ref,
Expand Down

0 comments on commit 9b3148a

Please sign in to comment.