question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to clean up with useEffect (Warning: Can't perform a React state update on an unmounted component.)

See original GitHub issue

What 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kaaysocommented, Jul 1, 2019

You must declare <ToastContainer /> in root of your app. For example in App component :

// app.jsx

import React, { Component } from "react";
import { ToastContainer } from "react-toastify";

class App extends Component {

  render() {
    return (
      <div>
        <ToastContainer />
        <OtherComponent>
        ...
      </div>
    );
  }
}

export App;

// index.js

import App from "./app";
import React from "react";
import ReactDOM from "react-dom";
import registerServiceWorker from "./registerServiceWorker";

ReactDOM.render(
    <App />,
  document.getElementById(`root`),
);
registerServiceWorker();

Now, you can call toast function in children components.

0reactions
mattcarlottacommented, Dec 21, 2019

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


Working in combination with PR #409, setting a ref on the div and using it as a conditional will prevent unmounted nodes from having their state updated:

 ...

  componentDidMount() {
    eventManager
      .cancelEmit(ACTION.WILL_UNMOUNT)
      .on(ACTION.SHOW, (content, options) =>
        this.ref ? this.buildToast(content, options) : null
      )
      .on(ACTION.CLEAR, id =>
        !this.ref ? null : id == null ? this.clear() : this.removeToast(id)
      )
      .emit(ACTION.DID_MOUNT, this);
  }

 ...

 render() {
   return (
     <div ref={node => (this.ref = node)} className={`${RT_NAMESPACE}`}>
        {this.renderToast()}
      </div>
   );
 }

Working repo (with both #409 and #411 PRs combined and compiled): Edit React Toastify Bugs Fixed

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found