0.0.5 • Published 5 years ago

context-hooks v0.0.5

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

context-hooks

React Hooks for accessing state and dispatch from a store

Npm Version Month Downloads Bundle Size

Install

# Yarn
yarn add context-hooks

# NPM
npm install --save context-hooks

Quick Start

// bootstrap your app

import { StoreContext, createStore } from 'context-hooks'

ReactDOM.render(
  <StoreContext.Provider value={store}>
    <App />
  </StoreContext.Provider>,
  document.getElementById('root'),
)
// individual components

import { useReducers, useConntect } from 'context-hooks'

const App = () => {
  const state = useConntect((state) => ({
    count: state.counter.count,
  }))
  
  const { counter } = useReducers()

  return (
    <Fragment>
      Count: {count}<br />

        <button onClick={() => counter.increase()}>increase count</button>
        <button onClick={() => counter.decrease()}>decrease count</button>
    </Fragment>
  )
}

Usage

NOTE: React hooks require react and react-dom version 16.8.0 or higher.

Example

// reducers

export default {
  counter: {
    initialState: {
      count: 0,
    },
    increase = (state) => ({ count: state.count + 1 }),
    decrease = (state) => ({ count: state.count - 1 }),
  },
}
// bootstrap your app

import { StoreContext, createStore } from 'context-hooks'
import reducers from './reducers'

const store = createStore(reducers)

ReactDOM.render(
  <StoreContext.Provider value={store}>
    <App />
  </StoreContext.Provider>,
  document.getElementById('root'),
)
// individual components

import { useReducers, useConntect } from 'context-hooks'

const App = () => {
  const state = useConntect((state) => ({
    count: state.counter.count,
  }))
  
  const { counter } = useReducers()

  return (
    <Fragment>
      Count: {count}<br />

        <button onClick={() => counter.increase()}>increase count</button>
        <button onClick={() => counter.decrease()}>decrease count</button>
    </Fragment>
  )
}
0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago