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

@preact-hooks/unistore

v1.1.2

Published

A custom Preact hook to wire up components to Unistore.

Downloads

172

Readme

Inspired by react-redux and redux-zero

version MIT License

A custom Preact hook to wire up components to Unistore.

Table of Contents

Installation

This package has Preact 10+ and Unistore 3.4+ as peer dependencies.

npm install @preact-hooks/unistore or yarn add @preact-hooks/unistore

You can also load it via the unpkg CDN

https://unpkg.com/@preact-hooks/unistore will download the latest UMD bundle.

Docs

Setup

To get started wrap your app in a Provider component to make the store available throughout the entire component tree.

import { StoreProvider }  from '@preact-hooks/unistore'

import createStore from 'unistore';
import devtools from 'unistore/devtools';

const initialStore = {}

const store = (process.env.NODE_ENV === 'production') 
  ? createStore(initialStore) 
  : devtools(createStore(initialStore))

function App() {
  return (
    <StoreProvider value={store}>
      // ...  
    </StoreProvider>
  )
}

🎉 Now you are ready to start using the hooks listed below in your function components.

useStore

const store = useStore()

This hook returns a reference to the same Unistore store that was passed into the StoreProvider component.

This hook should probably not be used frequently. Prefer useSelector as your primary choice. However, this may be useful for less common scenarios that do require access to the store.

import { useStore } from '@preact-hooks/unistore'

function MyComponent() {
  const store = useStore()

  // ...
}

useSelector

// You can pass in a selector function such as (s) => s.prop
// Or, you can pass in a string such as 'foo,bar' which will return { foo, bar }
const result = useSelector(selector, equalityFn?)

To learn about this hook checkout the amazing docs over at React Redux.

Pay attention to:

There is a convenient equality function called shallowEqual included. You can use this with the useSelector hook if you require it. If you're not sure when you'd need this then click on the link above titled "Equality Comparisons and Updates".

import shallowEqual from '@preact-hooks/unistore/shallowEqual'

function MyComponent() {
  const result = useSelector(selectorFn, shallowEqual)

  // ...
}

You could also use something like the Lodash _.isEqual() utility or Immutable.js's comparison capabilities. It's up to you how the equality check is performed.

You can also checkout Reselect which is a "selector" library for Redux.

useAction

const action = useAction(actionFn)

This hook is used to create Unistore actions. The function passed into the hook is identical to how you would create an action for Unistore, they receive the current state and return a state update.

import { useAction } from '@preact-hooks/unistore'

import { h } from 'preact'

function Counter() {
  const increment = useAction(({ count }) => ({ count: count + 1 }));
  
  return (<button onClick={increment}>Click me</button>);
}

If your actions are defined in another file then just import it and pass it through.

// ...

import { increment } from 'myActionsFile.js'

function Counter() {
  const increment = useAction(increment);

  // ...  
}

LICENSE

MIT