Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SvgUri can set state after unmount #1706

Closed
kjossendal opened this issue Feb 24, 2022 · 6 comments
Closed

SvgUri can set state after unmount #1706

kjossendal opened this issue Feb 24, 2022 · 6 comments
Labels
Close when stale This issue is going to be closed when there is no activity for a while Missing repro This issue need minimum repro scenario

Comments

@kjossendal
Copy link

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} />;
}
@kjossendal
Copy link
Author

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} />;
}

@lyqht
Copy link

lyqht commented 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)

@ansmlc
Copy link

ansmlc commented May 7, 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)

Same here. But the error didn't say where, took forever to pinpoint it to SvgUri.

@lyqht
Copy link

lyqht commented Jul 12, 2022

any updates on this?

@atlj
Copy link

atlj commented Jul 19, 2022

import React, { useEffect, useState } from 'react';
import { UriProps, SvgXml } from 'react-native-svg';

export function SvgUri(props: UriProps) {
  const [xml, setXml] = useState<null | string>(null);

  useEffect(
    function fetchXml() {
      const fetchController = new AbortController();

      if (props.uri !== null)
        fetch(props.uri, { signal: fetchController.signal })
          .then((response) => response.text())
          .then(setXml);

      return () => fetchController.abort();
    },
    [props.uri]
  );
  return <SvgXml xml={xml} override={props} />;
}

here is a solution derived from @Balintataw's

@bohdanprog
Copy link
Member

Hello,
Could you please provide a straightforward way to replicate how I can receive that warning?
Thank you.

@bohdanprog bohdanprog added Missing repro This issue need minimum repro scenario Close when stale This issue is going to be closed when there is no activity for a while labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Close when stale This issue is going to be closed when there is no activity for a while Missing repro This issue need minimum repro scenario
Projects
None yet
Development

No branches or pull requests

5 participants