Giter Site home page Giter Site logo

preact-hooks-unistore's Introduction

Preact Hooks - Unistore

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

preact-hooks-unistore's People

Contributors

mihar-22 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.