0.0.11 • Published 5 years ago

react-shared-hooks v0.0.11

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

react-shared-hooks

POC of global react hooks. Makes it possible to use global states based on hooks.

Usage:

import * as React from 'react'
import { createSharedHook, useSharedHooksState, withSharedHooks } from 'react-shared-hooks'

const { useState } = React

const counter = createSharedHook(useState, { count: 0 })

const Counter = () => {
  const [state, setState] = useSharedHooksState(state => state.counter)

  return (
    <div>
      {state.count}
      <button onClick={() => setState({ count: state.count + 1 })}>+</button>
    </div>
  )
}

const App = () => (
  <React.Fragment>
    <Counter />
    <Counter />
  </React.Fragment>
)

export default withSharedHooks({ counter })(App)

API

createSharedHook(hook, ...args)

  • hook: function - react hook
  • ...args: any - arguments that will be passed at the time of the hook initialization

withSharedHooks(hooks)(Component)

  • hooks: object - object of created hooks
  • Component: React Component - wrapped component

useSharedHooksState(selector)

  • selector: function - selector function that receives object of all resolved hook states

Examples

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago