use-refs-map

1.0.0 • Public • Published

useRefsMap ( React hook )

NPM

What the purpose ?

useRefMap is a simple React Hook to keep the last values & objects references on a constant hashmap/object.

Ok, but what the purpose ?

When we define functions, the accessible variables are those that where present when the function was defined.
This force us to redefine functions on most redraw, or use complex state management & reducers.

This Hook, allow us to keep functions unique as they can access the last value throught the RefMap

Code ( so simple )

export default function useRefsMap( refs ) {
	const scope = React.useRef({}).current;
	
	Object.assign(scope, refs);
	
	return scope;
}

Sample

import useRefsMap from "use-refs-map";

export default ( {
	                 style,
	                 ...props
                 } ) => {
	const
		slideQuery                        = Hooks.useQuery(
			"Slides",
			{
				params: { orderBy: { _created: -1 } }
			}
		),
		[selectedIndex, setSelectedIndex] = React.useState(0),
		styles                            = React.useMemo(
			() => (
				{}
			),
			[]
		),
		locals                            = useRefsMap(
			{
				slideQuery,
				selectedIndex
			}
		),
		api                               = React.useMemo(
			() => (
				{
					doSomeStuff: () => {
						// locals.slideQuery is updated on any render
						// so functions in the api object does'nt need to be re-created
						// this avoid rerender / updating listeners 
					}
				}
			),
			[]
		);
	
	return <div className={"HomePage"}>
		{/*  */}
	</div>;
};

Readme

Keywords

none

Package Sidebar

Install

npm i use-refs-map

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

4.9 kB

Total Files

4

Last publish

Collaborators

  • n8tz