How to clean up with useEffect (Warning: Can't perform a React state update on an unmounted component.)
See original GitHub issueWhat is the proper way to clean up via useEffect after switching page?
import React, { useEffect } from 'react'
import { ToastContainer, toast, Flip } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
import { css } from 'glamor'
export default function Toast() {
const goal = toast(<ToastText />, {
toastId: 'goal',
className: css({
position: 'absolute',
top: '80px',
boxShadow: 'none',
left: '0px',
background: 'rgba(46, 139, 87, 0.9)',
width: '100vw',
}),
bodyClassName: css({
color: 'white',
fontSize: '30px',
}),
progressClassName: css({
background: 'white',
height: '2px',
}),
})
console.log(toast.isActive(goal))
useEffect(() => {
return () => toast.dismiss()
}, [])
return (
toast.isActive(goal) || (
<ToastContainer
autoClose={5000}
transition={Flip}
position={toast.POSITION.TOP_LEFT}
/>
)
)
}
function ToastText() {
return (
<>
<h6
style={{
margin: 0,
fontSize: '15px',
fontWeight: 'bold',
paddingBottom: '5px',
}}
>
Your daily goal reminder
</h6>
<p style={{ margin: 0, fontSize: '12px' }}>
46 min left to achieve your daily goal. Go for it!
</p>
</>
)
}```
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (2 by maintainers)
Top Results From Across the Web
React-hooks. Can't perform a React state update on an ...
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application....
Read more >React: Prevent state updates on unmounted components
Warning : Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in...
Read more >Can't perform a React state update on an unmounted ... - GitHub
Warning : Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in...
Read more >How to get rid of “Can't perform a React state update ... - Medium
Warning : Can't perform a React state update on an unmounted component. This is a no-op, ... Here the useEffect clean-up comes into...
Read more >How to Fix "cannot update unmounted component" Warning ...
When you attempt to update the state of a component after its been unmounted, React will warn you that you should not do...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You must declare
<ToastContainer />
in root of your app. For example in App component :Now, you can call toast function in children components.
This setState warning also occurs when using Webpack’s inline hot reloading. It appears that the
eventManager.on
calls in the ToastContainer are being triggered in an unmounted/stale component.Here’s a small reproduction of the warning (all 3 messages render as intended; remove the text, then check the console to see the React warning and now only 1 message displays):![Edit React Toastify Bug](https://codesandbox.io/static/img/play-codesandbox.svg)
Working in combination with PR #409, setting a
ref
on thediv
and using it as a conditional will prevent unmounted nodes from having their state updated:Working repo (with both #409 and #411 PRs combined and compiled):![Edit React Toastify Bugs Fixed](https://codesandbox.io/static/img/play-codesandbox.svg)