use-dynamic-refs
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

React use-dynamic-refs

Made with create-react-library

NPM JavaScript Style Guide

Install

npm install --save use-dynamic-refs

Usage

import React, { useEffect } from 'react';
import useDynamicRefs from 'use-dynamic-refs';
 
const Example = () =>  {
  const foo = ['random_id_1', 'random_id_2'];
  const [getRef, setRef] =  useDynamicRefs();
 
  useEffect(() => {
    // Get ref for specific ID 
    const id1 = getRef('random_id_1');
    console.log(id1)
  }, [])
 
    return ( 
      <>
        {/* Simple set ref. */}
        <span ref={setRef('random_id_3')}></span>
 
         {/*  Set refs dynamically in Array.map() */}
        { foo.map((eachId, idx) => (
          <div ref={setRef(eachId)}> Hello {each} </div>))}
      </>
    )
}
 
export default Example;

License

MIT © fitzmode

Readme

Keywords

none

Package Sidebar

Install

npm i use-dynamic-refs

Weekly Downloads

2,969

Version

1.0.0

License

MIT

Unpacked Size

6.66 kB

Total Files

9

Last publish

Collaborators

  • fitzmode