Git Product home page Git Product logo

react-register-nodes's Introduction

react-register-nodes

Register DOM nodes within a context. Helpful for UI where many siblings need to know about each other. A common example is scrolling to the first error after a form submission.

Installation

yarn add react-register-nodes

Examples

Usage

import * as React from "react";
import { render } from "react-dom";
import {
  NodeManager,
  useNodes,
  useRegisteredRef
} from "react-register-nodes";


const Example = () => {
  // useRegisteredRef will return a ref to be used on the DOM node you'd like
  // to register. It accepts a string key to register the node under.
  const ref = useRegisteredRef("shallow");

  // useNodes will return an object containing any DOM nodes we have assigned our refs to
  // in this case, our div will be mapped to the key 'shallow'
  const nodes = useNodes();

  return (
    <React.Fragment>
      <div ref={ref}>
        Register Me!
      </div>

      <div>
        Registered Nodes:
        <pre>{JSON.stringify(Object.keys(nodes), null, 2)}</pre>
      </div>
    </React.Fragment>
  );
};

const rootElement = document.getElementById("root");
render(
  <div id="app">
    <NodeManager>
      <Example />
    </NodeManager>
  </div>,
  rootElement
);

API Reference

<NodeManager/>

This is the Context Provider. Must be above any components that call the use* hooks.

useRegisteredRef(key: string): HTMLElement | undefined

Returns a ref to be passed to your DOM node. The node assigned to ref.current will be registered with the nearest NodeManager. Accepts an id to serve as the key to register the node under.

useNodes(): { [key: string]: HTMLElement }

Returns an object of all registered nodes. Nodes are keyed by the key you passed to useRegisteredRef.

useOrderedNodes(sorter: (nodes: HTMLElement[]) => HTMLElement[]): HTMLElement[]

Returns all registered nodes in the order specified by sorter. Uses DOM order by default.

Author

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.