Giter VIP home page Giter VIP logo

Comments (2)

vasturiano avatar vasturiano commented on May 27, 2024

@egorairo you need to check whether your ref is defined before using it. It seems like in this case it is not. Specifically when you're using SSR this is quite important as refs tend to be defined only on the client side.

from react-force-graph.

samjusaitis avatar samjusaitis commented on May 27, 2024

@egorairo I ran into a similar problem using Next.js and trying to dynamically import this package.

Couple things that got it working for me:

  1. Had to ensure I was using a workaround like this one to ensure the ref was getting properly attached to the force graph.
  2. Even with this workaround, the ref wasn't available on the first render (unsure why). So I added further logic to check for this:
'use client';

import { useEffect, useReducer, useRef, useState } from 'react';
import { forceY } from 'd3-force';
import ForceGraph2D from './ForceGraph2D';

export const Graph = props => {
    const { data } = props;

    const graphRef = useRef(null);

    const [hasInitialisedForces, setHasInitialisedForces] = useState(false);
    const [tick, incrementTick] = useReducer(v => v + 1, 0);

    useEffect(() => {
        const graph = graphRef.current;

        if (!graph || hasInitialisedForces) return;

        graph.d3Force('center', null);
        graph.d3Force('directional', forceY(100));

        setHasInitialisedForces(true);
    }, [tick, hasInitialisedForces]);

    return (
        <ForceGraph2D
            ref={graphRef}
            graphData={data}
            onEngineTick={incrementTick}
        />
    );
};

from react-force-graph.

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.