Coder Social home page Coder Social logo

Comments (4)

illetid avatar illetid commented on September 26, 2024 1

Hi, for cases like this when you have some race conditions, we suggest you use React.useRef to check if the component is still mounted.

const isMounted = useRef(true);

  useEffect(
        () => {
          const chart = createChart(chartContainerRef.current, chartOptions)
          candles = await fetchCandles()
          if (isMounted.current) {
             // Do whatever you want to do with chart
             const mainSeries = chart.addCandlestickSeries(candles)
          }

            return () => {
                window.removeEventListener('resize', handleResize);
                isMounted.current = false;
                chart.remove();
            };
        },
        [deps]
    );

Also, you could use AbortController to prevent fetching the data if the component is already unmounted

from lightweight-charts.

0x33dm avatar 0x33dm commented on September 26, 2024 1

The library is client-side only, so you shouldn't render it on the server;
i'm not rendering on the server, on the server i show a "skeleton"

in the case of the modern Next.js, you should use the 'use client' directive. Regarding accessing some properties after remove(), we don't have plans to change the current behavior, at least in the near future.

thank you, your suggestions and explanations were very helpful.

from lightweight-charts.

0x33dm avatar 0x33dm commented on September 26, 2024

Hi, for cases like this when you have some race conditions, we suggest you use React.useRef to check if the component is still mounted.

Also, you could use AbortController to prevent fetching the data if the component is already unmounted

Thanks for the suggestions @illetid definitely more elegant than my ['isRemoving'] hack.

Still, due the fact this happens with the most basic react example on the documentation when used on Next.js ( due to the re-render nature of hydration, etcs ) wouldn't it make sense to have the library not trying to read some _internal* variable after the remove() method has been called?

from lightweight-charts.

illetid avatar illetid commented on September 26, 2024

The library is client-side only, so you shouldn't render it on the server; in the case of the modern Next.js, you should use the 'use client' directive. Regarding accessing some properties after remove(), we don't have plans to change the current behavior, at least in the near future.

from lightweight-charts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.