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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-dataflow-editor

v0.1.6

Published

A generic drag-and-drop dataflow editor for React

Readme

react-dataflow-editor

standard-readme compliant license NPM version TypeScript types lines of code

A generic drag-and-drop dataflow editor for React.

✨ You can read about the design of this component in this blog post!

Table of Contents

Install

npm i react-dataflow-editor

Usage

Schema

To use the editor, you must first define a static schema listing the kinds of nodes you want to use. Here's an example schema:

const kinds = {
	add: {
		name: "Addition",
		inputs: { a: null, b: null },
		outputs: { sum: null },
		backgroundColor: "lavender",
	},
	div: {
		name: "Division",
		inputs: { dividend: null, divisor: null },
		outputs: { quotient: null, remainder: null },
		backgroundColor: "darksalmon",
	},
}

This schema declares two kinds of nodes - an add node with two inputs a and b and one output sum, and a div node with two inputs dividend and divisor and two outputs quotient and remainder.

A schema is an object assignable to the type Record<string, { name: string; inputs: Record<string, null>; outputs: Record<string, null>; backgroundColor: string }>. However the schema must be defined without type annotations (and with TypeScript's strict mode enabled); this is because the editor is designed to leverage TypeScript's default type assignment rules to derive a more specific concrete typing of each schema that it will use to paramerize the editor component.

Editor

Once you've defined your schema (with no type annotations), you can pass it into the Editor component, along with a state value and a dispatch method. Editor is a controlled component - meaning the editor always renders the value of the current state prop, no matter what - but it doesn't use an onChange: (newState) => void callback like most controlled React components. Instead, it uses a dispatch callback that gets invoked with individual actions when the user tries to create/delete/move nodes or edges.

If all you want is to get the new state value on every change, you should use the exported makeReducer method in conjunction with React's useReducer hook, like in this example:

import React, { useReducer } from "react"
import {
	Editor,
	EditorState,
	GetSchema,
	EditorAction,
	makeReducer,
} from "react-dataflow-editor"

// Derive a concrete type-level schema from the kinds catalog
type S = GetSchema<typeof kinds>

interface MyEditorProps {
	initialValue?: EditorState<S>
}

const defaultInitialValue: EditorState<S> = {
	nodes: {},
	edges: {},
	focus: null,
}

function MyEditor(props: MyEditorProps) {
	const reducer = makeReducer(kinds)
	const [state, dispatch] = useReducer(
		reducer,
		props.initialValue || defaultInitialValue
	)

	return <Editor<S> kinds={kinds} state={state} dispatch={dispatch} />
}

If you want to take more fine-grained control over the editor actions - for example, if you wanted to prevent the user from deleting certain nodes - you can write you own dispatch method with your own logic inside it. In that case, you'll probably want to make use of the exported reduce method instead.

The kinds, dispatch, and options props provided to the Editor component must not change - the editor will not update to reflect new values of these props. Only their initial values will be used.

Viewer

If you want to render a read-only version of the editor, use the separate Viewer component. The viewer component takes all the same props as the editor component, including a dispatch callback, but the only actions that it will get invoked with are Focus actions. If you want users to be able to and deselect nodes and edges (and if you want to use the currently-selected node or edge in your application), you still need to use the useReducer hook the same way.

Demo

Contributing

PRs accepted!

I'm very interested in improving the real-world usability of the library. In particular I don't really know how to expose control over styling and layout in a useful way, so if you're trying to use this component I'd love to hear what kind of interface you'd like to have. Please open an issue to discuss this!

License

MIT © 2021 Joel Gustafson