Skip to content

useUnmounted does not work correctly #77

Description

@gaeaehrlich

useMounted

Explanation

Code example

const Element = () => {
    const [state, setState] = useState('text');
    const unmounted = useUnmounted();

    useEffect(() => {
        setTimeout(() => {
            !unmounted && setState('no text')
        }, 6000);
    }, [unmounted, state])

    return <div>{state}</div>
}

export default () => {
    const [mount, setMount] = useState(true);
    setTimeout(() => setMount(false), 5000);

    return <>{mount && <Element/>}</>
}
  • What is the expected behavior?
    When using useUnmounted the returned value should be true if component has unmounted.

  • What is happening instead?
    The value remains false.

  • What error message are you getting?
    Warning: 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. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions