Skip to content

Commit

Permalink
remove dependency on redux-updeep
Browse files Browse the repository at this point in the history
  • Loading branch information
harryhope committed Feb 15, 2021
1 parent dffee90 commit 7f1c433
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const reducer = createHookedOnReducer()
const store = createStore(reducer, {})
```

### Provider
### Provider
Hooked on Redux leverages the `<Provider />` component from `react-redux`. If you've ever used Redux with React before you are probably already familiar with this step. In fact, most of this code should look identical to the [react-redux](https://react-redux.js.org/introduction/quick-start#provider) quick start guide.

```jsx
Expand Down Expand Up @@ -143,4 +143,4 @@ useHookedOnState(selector, defaultState, options)
**Returns:** _(array)_ `[value, updateValue]` This function returns a "tuple" much like [`useState`](https://reactjs.org/docs/hooks-state.html). The first array element `value` is the value at the slice of state. The second element of the array `updateValue` is a function that accepts a single parameter that updates the global state at the slice of state specified by `selector`.

## Prior Art
Hooked on Redux builds very heavily on existing libraries and concepts. Under-the-hood it leverages [updeep](https://github.com/substantial/updeep) and [redux-updeep](https://github.com/algolia/redux-updeep) to do most of the work updating the state tree, and the library is particularly inspired by redux-updeep: See [How we reduced boilerplate and handled asynchronous actions with redux](https://blog.algolia.com/how-we-reduced-boilerplate-and-handled-asynchronous-actions-with-redux/).
Hooked on Redux is inspired by (and used to leverage) [updeep](https://github.com/substantial/updeep) and [redux-updeep](https://github.com/algolia/redux-updeep). In particular, this article: [How we reduced boilerplate and handled asynchronous actions with redux](https://blog.algolia.com/how-we-reduced-boilerplate-and-handled-asynchronous-actions-with-redux/).
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h3 id="usehookedonstate">useHookedOnState</h3>
<p>&bull; <code>rootPath</code>: <em>(string)</em> If the Hooked on Redux reducer is not at the root level of your store, you must specify the subpath it exists on with this parameter. This usually happens if you integrate Hooked on Redux into a larger Redux codebase using something like <code>combineReducers</code>. For instance, if the Hooked on Redux reducer is added to the combined reducers with the name <code>myReducer</code>, then <code>rootPath</code> should be <code>myReducer</code> as well. For a nested path you may specify a path such as <code>path.to.my.store.</code></p>
<p><strong>Returns:</strong> <em>(array)</em> <code>[value, updateValue]</code> This function returns a "tuple" much like <a href="https://reactjs.org/docs/hooks-state.html"><code>useState</code></a>. The first array element <code>value</code> is the value at the slice of state. The second element of the array <code>updateValue</code> is a function that accepts a single parameter that updates the global state at the slice of state specified by <code>selector</code>.</p>
<h2 id="prior-art">Prior Art</h2>
<p>Hooked on Redux builds very heavily on existing libraries and concepts. Under-the-hood it leverages <a href="https://github.com/substantial/updeep">updeep</a> and <a href="https://github.com/algolia/redux-updeep">redux-updeep</a> to do most of the work updating the state tree, and the library is particularly inspired by redux-updeep: See <a href="https://blog.algolia.com/how-we-reduced-boilerplate-and-handled-asynchronous-actions-with-redux/">How we reduced boilerplate and handled asynchronous actions with Redux</a>.</p></main>
<p>Hooked on Redux is inspired by (and used to leverage) <a href="https://github.com/substantial/updeep">updeep</a> and <a href="https://github.com/algolia/redux-updeep">redux-updeep</a>. In particular, this article: <a href="https://blog.algolia.com/how-we-reduced-boilerplate-and-handled-asynchronous-actions-with-redux/">How we reduced boilerplate and handled asynchronous actions with redux</a>.</p></main>
<footer>
Copyright &copy; 2021 Harry Hope. All rights reserved.
</footer>
Expand Down
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import get from 'lodash/get'
import set from 'lodash/set'
import cloneDeep from 'lodash/cloneDeep'
import isString from 'lodash/isString'
import isArray from 'lodash/isArray'
import createReducer from 'redux-updeep'
import { useSelector, useDispatch } from 'react-redux'

export const createHookedOnReducer = (initialState = {}, namespace = 'HOOKED_ON_REDUX', handlers = {}) =>
createReducer(namespace, initialState, handlers)
const defaultReducer = (state, { payload, path = '' }) => {
set(state, path, payload)
return cloneDeep(state)
}

export const createHookedOnReducer = (initialState = {}, namespace = 'HOOKED_ON_REDUX', handlers = {}) => {
const namespaceRegexp = new RegExp(`^${namespace}/`)
return function hookedOnReducer (state = initialState, action) {
if (Object.prototype.hasOwnProperty.call(handlers, action.type)) {
return handlers[action.type](state, action)
} else if (namespaceRegexp.test(action.type)) {
return defaultReducer(state, action)
}
return state
}
}

export const useHookedOnState = (selector, defaultState, options = {}) => {
const namespace = isString(options) ? options : get(options, 'namespace', 'HOOKED_ON_REDUX')
Expand Down
22 changes: 2 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
},
"homepage": "https://github.com/harryhope/hooked-on-redux#readme",
"dependencies": {
"lodash": "^4.17.15",
"redux-updeep": "^0.1.1"
"lodash": "^4.17.15"
},
"peerDependencies": {
"react-redux": "^7.2.0"
Expand Down

0 comments on commit 7f1c433

Please sign in to comment.