npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-d3

v0.1.6

Published

A small React hook to use D3 in declarative way, for data visualization & flexible animation.

Downloads

26

Readme

react-use-d3

npm check demo

A small React hook to use D3 in declarative way, for data visualization & flexible animation.

NOTE: I recommend to use my new library https://github.com/inokawa/react-animatable

This is under development and APIs are not fixed

Why?

D3 is an excellent library to do data-driven visualization. React is a nice library to create user interfaces in data-driven way. So why not use them together?

Well, integrating D3 into React was tried by many. Someone uses a part of D3's possibilities in React but it looks that no one succeeded to port all of them. I think it's because of mismatches between D3 and React.

  • React does DOM manipulation through virtual DOM but D3 does it directly with its own data binding system.
  • React is only for UI but D3 has everything for visualization.
  • React uses JSX but D3 doesn't (React's hook feels similar to D3 way in my opinion).
  • React's reconciliation misses some values interpolated by D3.

I'm trying to give a nice intermediate with this lib. Almost all syntaxes of D3 would work. The elements created with D3 are transformed into React Elements and handled by React's reconciliation. Attribute updates are applied directly through ref for smooth animation.

The core of this lib is based on react-faux-dom, but highly customized to fit to the lifecycle of React and handle D3's transition correctly. Also rewritten in TypeScript and fixed to support newest D3.

I'm trying to support D3's usecases as much as possible. If you notice some problems, I would appreciate if you could report it in a issue.

Demo

https://inokawa.github.io/react-use-d3/

Install

npm install react-use-d3

Requirements

  • react >= 16.8
  • d3 >= 4

Usage

import * as d3 from "d3";
import { useD3 } from "react-use-d3";

export const Component = ({ width, height, data }) => {
  const svg = useD3(
    (create) => {
      // create('foo') inits virtual element which accepts D3's mutations
      const svg = d3
        .select(create("svg"))
        .attr("width", width)
        .attr("height", height);
      svg
        .append("text")
        .attr("x", width / 2)
        .attr("y", height / 2)
        .attr("fill", "black")
        .text(data);
      return svg;
    },
    // deps of useD3 hook
    // useD3 hook runs if some of deps are updated like useMemo / useEffect
    [width, height, data]
  );
  // el.toReact() transforms virtual element into React element
  return <div>{svg.node().toReact()}</div>;
};

Limitations

TODO

TODOs

  • [ ] Fix API
  • [ ] Confirm that it works with...
    • [x] d3-selection
    • [x] d3-transition
    • [x] d3-scale
    • [x] d3-interpolate
    • [ ] d3-hierarchy
    • [ ] d3-shape
    • [ ] d3-path
    • [ ] d3-chord
    • [ ] d3-axis
    • [x] d3-drag
    • [ ] d3-brush
    • [ ] d3-zoom
    • [x] d3-force
    • [ ] d3-contour
    • [ ] d3-geo
    • [ ] d3-tile
    • [ ] d3-quadtree
    • [ ] d3-delaunay
    • [ ] d3-dag
    • etc.
  • [ ] Support canvas
  • [ ] Support React Native
  • [ ] Consider examples
  • [ ] Consider dependencies
  • [ ] Consider tests

Inspirations