Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Dynamically add nodes into react-force-graph-D3.js

P.S: I'm new to D3.js and force-graphs so please bare with me.

I would like to add nodes dynamically to the graph. I'm having hard time understanding how to do this. Please find my code below:

/src/index.js

import React, { useRef, useEffect } from "react";
import ReactDOM from "react-dom";
import ForceGraph2D from "react-force-graph-2d";
import "./styles.css";

var data = {
  nodes: [],
  links: []
};

function App() {
  const forceRef = useRef(null);
  useEffect(() => {
    for (let i = 0; i < 5; i++) {
      data.nodes.push({ id: i.toString()});
    }
    console.log(data);
    forceRef.current.d3Force("charge").strength(-400);
  });
  return (
    <ForceGraph2D
      graphData={data}
      nodeLabel="id"
      linkCurvature="curvature"
      enablePointerInteraction={true}
      linkDirectionalParticleWidth={1}
      ref={forceRef}
    />
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

If you would like to play around with code, please use this link for the sandbox. Graph nodes are not even visible on the screen. Is there something that I'm missing out on? It would be great if someone can update the code for better understanding.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!