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-unistore

v0.0.4

Published

778b connector between React and unistore

Downloads

535

Readme

npm

react-unistore

A 778b connector between React and unistore.

unistore already has great support for connecting with React by itself. However at time of writing it does not have support for React Hooks. This package aims to provide this capability, extending the API with something close to Redux’s React Hooks API.

Install

$ yarn add unistore react-unistore
# OR
$ npm install --save unistore react-unistore

API

Provider

Provider exposes a store to context. Required for all other functions to work.

Generally an entire application is wrapped in a single <Provider> at the root.

export default () => (
  <Provider value={store}>
    <App />
  </Provider>
);

useAction

Used to bind an action to the store.

const setUsername = useAction((state, username) => ({
  user: { ...state.user, username },
}));

useSelector

Used to extract values from the store.

const user = useSelector(state => state.user);

useStore

Used to access the store itself. Where possible use useAction and useSelector rather than accessing the store directly.

const store = useStore();

connect

Pre-hooks method of connecting to the store. See unistore docs for full details.

Usage (TypeScript)

Create your State. Whilst not necessary it can be helpful to wrap useSelector and useAction with your State: store.ts

import {
  Provider,
  TypedUseAction,
  TypedUseSelector,
  useAction as _useAction,
  useSelector as _useSelector,
} from "react-unistore";

export interface State {
  user: {
    firstName?: string;
  };
}

export const useSelector: TypedUseSelector<State> = _useSelector;
export const useAction: TypedUseAction<State> = _useAction;

export { Provider };

client.tsx

import { createStore, Provider } from "react-unistore";

const initialState = {
  user: {},
};

const store = createStore(initialState);

ReactDOM.render(
  <Provider value={store}>
    <App />
  </Provider>,
  document.getElementById("root")
);

ChildComponent.tsx

import { useAction, useSelector } from "./store";

export default function ChildComponent() {
  const user = useSelector(state => state.user);
  const setFirstName = useAction((state, firstName: string) => ({
    user: { ...state, firstName },
  }));
  return (
    <div>
      <span>Hi {user.firstName || "you"}</span>
      <button onClick={() => setFirstName("Fred")}>Update</button>
    </div>
  );
}

Migrating from unistore/react

If you are migrating from unistore/react to be able to use functionality available in this package you should find the API fully backwards compatiable. Simply^ change any imports from:

import { Provider, connect } from "unistore/react";

To:

import { Provider, connect } from "react-unistore";

^ With one exception. To align with the standard React Context API patterns the Provider must be passed as the 'value' prop.

export default () => (
-  <Provider store={store}>
+  <Provider value={store}>
    <App />
  </Provider>
);

Package Size of 778 Bytes

Raw File Size (ES6 version): 3.51 KiB
Raw File Size (ES5 version): 4.00 KiB
Minified + Gzip (ES6 version): 778 Bytes
Minified + Gzip (ES5 version): 864 Bytes