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-store-hook

v0.1.0

Published

Simple hooks-based state management for React

Downloads

5

Readme

React-Store-Hook

Simple hooks-based state management for React

Like unstated but with hooks

⚠️ Warning: React hooks are not part of a stable React release yet, so use this library only for experiments

Installation

npm install react-store-hook

Example

import React from 'react';
import ReactDOM from 'react-dom';
import {Container, useStore} from 'react-store-hook';

class CounterContainer extends Container {
  state = {
    count: 0,
  };

  increment = () => {
    this.setState({count: this.state.count + 1});
  };

  decrement = () => {
    this.setState({count: this.state.count - 1});
  };
}
const counter = new CounterContainer();

function Counter() {
  const [state, setState] = useStore(counter);

  const reset = () => setState({count: 0});

  return (
    <div>
      <button onClick={counter.decrement}>-</button>
      <span>{state.count}</span>
      <button onClick={counter.increment}>+</button>
      <button onClick={reset}>reset</button>
    </div>
  );
}

ReactDOM.render(<Counter />, document.getElementById('root'));

For more examples, see the example/ directory.

Guide

Unstated is awesome, but doesn't really use hooks.
Can we build something similar to unstated with hooks to make something even nicer?

Introducing React-Store-Hook

I really like unstated. I really like hooks. I wanted a simple hook-based app state management solution. This is why I've built React-Store-Hook.

React-Store-Hook is built on top of React components, context and hooks and patterns surrounding those elements.

It has three pieces:

Container

It's a place to store our state and some of the logic for updating it.

Container is a very simple class which is meant to look just like React.Component but with only the state-related bits: this.state and this.setState.

class CounterContainer extends Container {
  state = {count: 0};
  increment = () => {
    this.setState({count: this.state.count + 1});
  };
  decrement = () => {
    this.setState({count: this.state.count - 1});
  };
}

Note that Containers are also event emitters that components subscribe to for updates. When you call setState it triggers components to re-render, so be careful not to mutate this.state directly or your components won't re-render.

setState()

setState() in Container mimics React's setState() method as closely as possible.

class CounterContainer extends Container {
  state = {count: 0};
  increment = () => {
    this.setState(
      state => {
        return {count: state.count + 1};
      },
      () => {
        console.log('Updated!');
      }
    );
  };
}
useStore

Next we'll need a piece to introduce our state back into the tree so that:

  • When state changes, our components re-render.
  • We can depend on our container's state.
  • We can call methods on our container.

For this we have the useStore hook which allows us to pass our container instances and receive references to state, update function and the store itself (useful when passing the store using Provider).

function Counter() {
  const [state, setState, store] = useStore(counterStore);

  return (
    <div>
      <span>{state.count}</span>
      <button onClick={counterStore.decrement}>-</button>
      <button onClick={counterStore.increment}>+</button>
    </div>
  );
}

useStore will automatically construct our container and listen for changes.

<Provider>

The final optional piece that React-Store-Hook has is <Provider> component. It uses context to pass a given store instance to all the components down the tree.

render(
  <Provider store={counterStore}>
    <Counter />
  </Provider>
);

Once you'd wrapped your components with <Provider> you can simply use useStore hook without any arguments:

function Counter() {
  const [state, setState, store] = useStore();

  return <div>{state.count}</div>;
}

Testing

Whenever we consider the way that we write the state in our apps we should be thinking about testing.

We want to make sure that our state containers have a clean way

Well because our containers are very simple classes, we can construct them in tests and assert different things about them very easily.

test('counter', async () => {
  let counter = new CounterContainer();
  assert(counter.state.count === 0);

  counter.increment();
  assert(counter.state.count === 1);

  counter.decrement();
  assert(counter.state.count === 0);
});

Related