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.

SvgUri can set state after unmount

See original GitHub issue

Using v: 12.1.0 RN: 0.6.6.0

Essentially this part defining SvgUri can attempt to set state after the component has unmounted which will throw memory leak warnings with a call to setXml. Maybe need a mount safe setState hook?

export function SvgUri(props: UriProps) {
  const { onError = err, uri } = props;
  const [xml, setXml] = useState<string | null>(null);
  useEffect(() => {
    uri
      ? fetchText(uri)
          .then(setXml)
          .catch(onError)
      : setXml(null);
  }, [onError, uri]);
  return <SvgXml xml={xml} override={props} />;
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

5reactions
lyqhtcommented, Feb 27, 2022

getting the error too with a component like this

<IconButton
  icon={
      <Icon
          as={
              <SvgUri
                  width="48"
                  height="48"
                  uri={
                      "https://api.iconify.design/icon-park-outline:tea-drink.svg"
                  }
              />
          }
      />
  } 
/>
 ERROR  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.
    in SvgUri (at HomeScreen.tsx:48)
5reactions
Balintatawcommented, Feb 24, 2022

So far, patching with something like this works well for my case. fetchText is also used in conjunction with a setState call in the SvgFromUri class so this is just a temporary fix.

export function SvgUri(props: UriProps) {
  const { onError = err, uri } = props;
  const [xml, setXml] = useState<string | null>(null);
  useEffect(() => {
    let isMounted = true;
    uri
      ? fetchText(uri)
          .then((resp) => {
            if (isMounted) {
              setXml(resp)
            }
          })
          .catch(onError)
      : setXml(null);
      return () => {
        isMounted = false
      }
  }, [onError, uri]);
  return <SvgXml xml={xml} override={props} />;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

SvgUri can set state after unmount · Issue #1706 - GitHub
Essentially this part defining SvgUri can attempt to set state after the component has unmounted which will throw memory leak warnings with ...
Read more >
How to cancel SvgUri request when component will unmount
To fix, cancel all subscriptions and asynchronous task in "a useEffect cleanup function". My component is: import React, { useEffect } from ' ......
Read more >
How to cancel SvgUri request when component will unmount ...
[Solved]-How to cancel SvgUri request when component will unmount-React Native. Search. score:0. import YourSvgFile from './YourSvgFile.svg'; <YourSvgFile />.
Read more >
Prevent React setState on unmounted Component
How to avoid the React warning: Can only update a mounted or mounting component. It usually means you have called setState on an...
Read more >
is it dangerous Can't perform a React state update on an ...
componentWillUnmount() { // fix Warning: Can't perform a React state update on an unmounted component this.setState = (state,callback)=>{ ...
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