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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-rooks

v1.5.2

Published

Store management for React and React Native with simple hooks !

Downloads

67

Readme

React rooks

Many packages exist for store management. Why shoud you use this new one ?

Are you searching for :

  • hook driven management ?
  • simple stores ?
  • multiple stores ?
  • typed stores ?
  • simple reducers ?
  • contextual stores ?
  • async management ?
  • without dependencies ?
  • light and quick package ?

Here you'll find everything you need.

Install

With lovely yarn:

yarn add react-rooks

or with npm:

npm install react-rooks

Usage

First, create a rook store

rook.ts:

import createRook from 'react-rooks';

// Some global/stored values.
export const [Rook, useRook] = createRook({
    storedUser: {},
    locale: 'en',
    title: 'My React app',
} as {
    storedUser: { id?: string; name?: string; /* whatever */ };
    locale: 'en' | 'fr';
    title: string;
});

createRook takes two arguments:

  • store (Record<string, any>): your stored object
  • reducers: one or more reducers for your store. Reducers can either be:
    • an object where each key is a key of store and each value is a reducer ;
    • a global reducer where every value is reduced.

A reducer takes two arguments: the new value, and the last value. It must return the reduced value.

Example of reducer

If you want to change your window title, you can reduce the changed title value:

import createRook from 'react-rooks';

// Some global/stored values.
export const [Rook, useRook] = createRook({ ... }, {
    title: (value: string) => {
        window.title = 'My React app | ' + value;

        return value; // Here we can even alterate the reduced value.
    }
});

// In a component:
/* ... */
    setTitle('Home page');
/* ... */

Returns

As you can see, createRook returns an array:

  • Rook is the context of your store. You must add it before using the hook useRook in your components. Rook can be nested, with a duplicated nested store.
  • useRook is a hook, that retrieve the current store an is similar to React.useState.

Usage with multiple rooks!

Instead of nesting your rooks, you can contain them all together:

// From:
export const App = () => (
	<FirstRook>
	    <SecondRook>
            {/* ...more nested rooks... */}
		        {/* Your app */}
            {/* ...more nested rooks... */}
	    </SecondRook>
	</FirstRook>
);

// To:
import { RookContainer } from 'react-rooks';

export const App = () => (
	<RookContainer rooks={[FirstRook, SecondRook, /* ...more rooks... */]}>
        {/* Your app */}
	</FirstRook>
);

Then, use it!

App.tsx:

import * as React from 'react';
import { Rook, useRook } from './rook';

export const ChangeLocale = () => {
	const [locale, setLocale] = useRook('locale');

	React.useEffect(() => setTimeout(() => setLocale('fr'), 2500), []);

	return null;
};

export const ShowLocale = () => {
	const [locale] = useRook('locale');

	// Log: 'en'.
	// After 2.5s, log: 'fr'.
	console.log(locale);

	return null;
};

export const App = () => (
	<Rook>
		<ChangeLocale />
		<ShowLocale />
	</Rook>
);

export default App;

Feel free and enjoy