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, memoize, useStore, useDispatch, useDeepEffect, combineReducers } from 'react-context-hooks-store';

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

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

export { Provider, connect, memoize, rootReducer, useStore, useDispatch, useDeepEffect, 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, memoize, useDeepEffect } from './store.js';

const Movie = ({ theme }) => {
  useDeepEffect(() => {
  }, [array, object]);
  
  return (
    <div>{theme}</div>
  )
}

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

// options
const memoPropsAreEqual = (prevProps, nextProps) => {
  if (nextProps.visible !== prevProps.visible) {
    return false
  }

  return true
};

export default connect(mapState)(memoize(Movie, memoPropsAreEqual))

Current Tags

  • 0.1.15                                ...           latest (4 months ago)

21 Versions

  • 0.1.15                                ...           4 months ago
  • 0.1.12                                ...           4 months ago
  • 0.1.11                                ...           4 months ago
  • 0.1.10                                ...           4 months ago
  • 0.1.9                                ...           4 months ago
  • 0.1.8                                ...           4 months ago
  • 0.1.7                                ...           4 months ago
  • 0.1.6                                ...           6 months ago
  • 0.1.5                                ...           7 months ago
  • 0.1.4                                ...           7 months ago
  • 0.1.2                                ...           7 months ago
  • 0.1.1                                ...           7 months ago
  • 0.1.0                                ...           a year ago
  • 0.0.8                                ...           a year ago
  • 0.0.7                                ...           a year ago
  • 0.0.6                                ...           a year ago
  • 0.0.5                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)

Copyright 2014 - 2017 © taobao.org |