react-context-hooks-store
A simple global store based on React Context and Hooks
Last updated 4 months ago by dotcody .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install react-context-hooks-store 
SYNC missed versions from official npm registry.

react-context-hooks-store

A simple global store based on React Context and Hooks

Installation

npm install react-context-hooks-store
or
yarn add react-context-hooks-store

Use It

// reducers/common.js
let initialState = {
  theme: 'white'
};

const reducer = (state = initialState, action) => {
  const { type, payload } = action;

  switch (type) {
    case `common/${constants.ACTION_TYPE.UPDATE_STATE}`:
      return {
        ...state,
        ...payload
      };
      
    default:
      return state;
  }
};

export default reducer
// store.js
import { Provider, connect, useStore, useDispatch, combineReducers } from 'react-context-hooks-store';

import common from './reducers/common';
import global from './reducers/global';

const rootReducer = combineReducers({
  global,
  common
});

export { Provider, connect, rootReducer, useStore, useDispatch, combineReducers }
// src/index.js
import { Provider, rootReducer } from './store.js';

const initialValue = {}

<Provider rootReducer={rootReducer} initialValue={initialValue}>
  <App />
</Provider>
// src/pages/movie.js
import { connect } from './store.js';

const Movie = ({ theme }) => {
  return (
    <div>{theme}</div>
  )
}

const mapState = ({ common: { theme } }) => ({
  theme
});

export default connect(mapState)(Movie)

Current Tags

  • 0.1.0                                ...           latest (4 months ago)

9 Versions

  • 0.1.0                                ...           4 months ago
  • 0.0.8                                ...           4 months ago
  • 0.0.7                                ...           4 months ago
  • 0.0.6                                ...           4 months ago
  • 0.0.5                                ...           4 months ago
  • 0.0.4                                ...           4 months ago
  • 0.0.3                                ...           4 months ago
  • 0.0.2                                ...           4 months ago
  • 0.0.1                                ...           4 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (1)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |