All Projects → palmerhq → react-register-nodes

palmerhq / react-register-nodes

Licence: MIT license
Register DOM Nodes in React

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to react-register-nodes

tacklebox
🎣React UX components for handling common interactions
Stars: ✭ 15 (-53.12%)
Mutual labels:  react-dom, react-hooks
react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (+162.5%)
Mutual labels:  react-dom, react-hooks
crud-app
❄️ A simple and beautiful CRUD application built with React.
Stars: ✭ 61 (+90.63%)
Mutual labels:  react-hooks
react-headless-tabs
Headless and highly flexible tab-like primitives built with react hooks
Stars: ✭ 107 (+234.38%)
Mutual labels:  react-hooks
starkgate-frontend
Bridge interface allows users to transfer ERC20 tokens from Ethereum to StarkNet and vice versa.
Stars: ✭ 75 (+134.38%)
Mutual labels:  react-hooks
re-use
⚛️ 🎣 A collection of hooks for ReasonReact
Stars: ✭ 27 (-15.62%)
Mutual labels:  react-hooks
react-cloud-music-typescript
基于React和Typescript搭建的cloud music项目
Stars: ✭ 14 (-56.25%)
Mutual labels:  react-hooks
use-pan-and-zoom
👆+🔎 React hook for panning and zooming a container
Stars: ✭ 57 (+78.13%)
Mutual labels:  react-hooks
NFT-Dapp-Boilerplate
A highly scalable NFT and DEFI boilerplate with pre added web3 and different wallets with a focus on performance and best practices
Stars: ✭ 51 (+59.38%)
Mutual labels:  react-hooks
react-warehouse
React resource loader implementation
Stars: ✭ 35 (+9.38%)
Mutual labels:  react-hooks
shaka-player-react
A simple React component wrapper for shaka-player
Stars: ✭ 79 (+146.88%)
Mutual labels:  react-hooks
react-suspense-playground
Stock Market News app w/ React Suspense
Stars: ✭ 43 (+34.38%)
Mutual labels:  react-dom
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+8878.13%)
Mutual labels:  react-hooks
veact
Mutable state enhancer library for React based on @vuejs
Stars: ✭ 62 (+93.75%)
Mutual labels:  react-hooks
frontend-toolbox
Frontend tools which we used in snappmarket v2
Stars: ✭ 37 (+15.63%)
Mutual labels:  react-hooks
react-hook-videojs
Easy React integration of Video.js using hooks.
Stars: ✭ 37 (+15.63%)
Mutual labels:  react-hooks
whatsapp-clone-react
Build a WhatsApp Clone with React JS and FireBase.
Stars: ✭ 38 (+18.75%)
Mutual labels:  react-hooks
react-use-scroll-position
A react hook to use scroll position
Stars: ✭ 45 (+40.63%)
Mutual labels:  react-hooks
react-weather-app
An attempt to make an ultimate weather app. In ReactJS, with React hooks and context.
Stars: ✭ 39 (+21.88%)
Mutual labels:  react-hooks
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (+28.13%)
Mutual labels:  react-hooks

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].