react-context-api-store
Advanced tools
Comparing version 1.0.10 to 1.0.11
@@ -6,4 +6,6 @@ 'use strict'; | ||
}); | ||
exports.connect = exports.StoreContext = undefined; | ||
exports.connect = undefined; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
@@ -15,2 +17,6 @@ | ||
var _propTypes = require('prop-types'); | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -91,4 +97,11 @@ | ||
return _ret2 = (_temp2 = (_this2 = _possibleConstructorReturn(this, (_ref2 = Provider.__proto__ || Object.getPrototypeOf(Provider)).call.apply(_ref2, [this].concat(args))), _this2), _this2.state = _extends({}, _this2.props.store), _this2.updateState = function (updatedState) { | ||
return _this2.setState(_extends({}, _this2.state, updatedState)); | ||
return _ret2 = (_temp2 = (_this2 = _possibleConstructorReturn(this, (_ref2 = Provider.__proto__ || Object.getPrototypeOf(Provider)).call.apply(_ref2, [this].concat(args))), _this2), _this2.state = _extends({}, _this2.props.store), _this2.persisted = false, _this2.updateState = function (updatedState) { | ||
var newState = _extends({}, _this2.state, updatedState); | ||
_this2.setState(newState); | ||
if (_this2.props.persist !== false) { | ||
_this2.props.persist.storage.removeItem(_this2.props.persist.key || 'react-context-api-store'); | ||
_this2.props.persist.storage.setItem(_this2.props.persist.key || 'react-context-api-store', JSON.stringify(newState)); | ||
} | ||
}, _this2.render = function () { | ||
@@ -106,2 +119,14 @@ return _react2.default.createElement( | ||
_createClass(Provider, [{ | ||
key: 'componentDidMount', | ||
value: function componentDidMount() { | ||
if (this.props.persist !== false && !this.persisted) { | ||
this.persisted = true; | ||
var savedStore = this.props.persist.storage.getItem(this.props.persist.key || 'react-context-api-store'); | ||
this.updateState(savedStore ? this.props.persist.statesToPersist(JSON.parse(savedStore)) : {}); | ||
} | ||
} | ||
}]); | ||
return Provider; | ||
@@ -112,4 +137,18 @@ }(_react2.default.Component); | ||
exports.StoreContext = StoreContext; | ||
Provider.propTypes = { | ||
children: _propTypes2.default.element.isRequired, | ||
store: _propTypes2.default.object.isRequired, | ||
persist: _propTypes2.default.oneOfType([_propTypes2.default.shape({ | ||
storage: _propTypes2.default.object.isRequired, | ||
statesToPersist: _propTypes2.default.func.isRequired, | ||
saveInitialState: _propTypes2.default.bool, | ||
key: _propTypes2.default.string | ||
}), _propTypes2.default.oneOf([false])]) | ||
}; | ||
Provider.defaultProps = { | ||
persist: false | ||
}; | ||
exports.connect = connect; | ||
exports.default = Provider; |
{ | ||
"name": "react-context-api-store", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "An HOC state management inpired by redux implemented using react's context api.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "babel src --out-dir lib/", | ||
"test": "npm run build && jest --coverage --updateSnapshot", | ||
"build": "rm -rf lib && babel src --out-dir lib && cp lib/index.js example/src/lib.js", | ||
"lint": "eslint src/**/*.js __tests__/**/*.js" | ||
@@ -41,3 +41,4 @@ }, | ||
"babel-cli": "^6.26.0", | ||
"babel-eslint": "^8.2.4", | ||
"babel-eslint": "^8.2.5", | ||
"babel-jest": "^23.0.1", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
@@ -50,4 +51,14 @@ "babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"eslint": "^4.19.1", | ||
"eslint-plugin-react": "^7.9.1" | ||
"eslint-plugin-react": "^7.9.1", | ||
"jest": "^23.1.0", | ||
"jest-localstorage-mock": "^2.2.0", | ||
"react-dom": "^16.4.1", | ||
"react-native": "^0.55.4", | ||
"react-test-renderer": "^16.4.1" | ||
}, | ||
"jest": { | ||
"setupFiles": [ | ||
"jest-localstorage-mock" | ||
] | ||
} | ||
} |
@@ -5,10 +5,7 @@ # Repo status? | ||
# react-context-api-store | ||
Seemless, lightweight, state management library that comes with asynchronous support out of the box. Inspired by Redux and Vuex. Built on top of [React's context api](https://reactjs.org/docs/context.html). | ||
Seemless, lightweight, state management library that supports async actions and state persisting out of the box. Inspired by Redux and Vuex. Built on top of [React's context api](https://reactjs.org/docs/context.html). | ||
# File size? | ||
5kb transpiled, not minified. | ||
6.7kb transpiled. Not minified. Not compressed. Not uglified. | ||
# Use case | ||
When you want a state management that's small and supports asynchronous actions out of the box. | ||
# Example | ||
@@ -290,4 +287,42 @@ https://aprilmintacpineda.github.io/react-context-api-store/#/ | ||
## Persisting states | ||
If you want to persist states, just provide a second property called `persist` which is an object that has the following shape: | ||
```js | ||
{ | ||
storage: AsyncStorage, // the storage of where to save the state | ||
statesToPersist: savedStore => { | ||
// do whatever you need to do here | ||
// then return the states that you want to save. | ||
// NOTE: This is not strict, meaning, you can even | ||
// create a new state here and it will still be saved | ||
return { | ||
someState: { ...savedStore.someState }, | ||
anotherState: [ ...savedStore.anotherState ], | ||
someValue: savedStore.someValue | ||
} | ||
} | ||
} | ||
``` | ||
**example snippet** | ||
```jsx | ||
<Provider store={store} persist={{ | ||
storage: window.localStorage, | ||
statesToPersist (savedStore) { | ||
return { ...savedStore }; | ||
} | ||
}}> | ||
``` | ||
In this case I'm passing in the `window.localStorage` as the storage but you are free to use whatever storage you need but it must have the following methods: | ||
- `getItem` which receives the `key` as the first parameter. | ||
- `setItem` which receives the `key` as the first parameter and `value` as the second parameter. | ||
- `removeItem` which receives the `key` as the first parameter. | ||
# Related | ||
- [inferno-context-api-store](https://github.com/aprilmintacpineda/inferno-context-api-store) inferno compatible version of the same thing. |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
495511
13083
0
326
16
1