Skip to content

Commit

Permalink
reduce bundle size by replacing updeep with Array.slice()
Browse files Browse the repository at this point in the history
  • Loading branch information
harryhope committed Dec 24, 2020
1 parent dff4dd0 commit fab3090
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
15 changes: 14 additions & 1 deletion hooked-on-redux.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const Counter = props => {

const TodoList = props => {
const [list, updateList] = useHookedOnState('list', ['milk'])
const mutateList = () => {
list.push('eggs')
list.push('bread')
list.push('taters')
updateList(list)
}
return (
<main>
<div data-testid='list'>
Expand All @@ -27,7 +33,11 @@ const TodoList = props => {
</div>
<button
data-testid='add'
onClick={() => updateList(list.concat(['eggs', 'bread', 'taters']))}
onClick={mutateList}
/>
<button
data-testid='replace'
onClick={() => updateList(['cookies', 'candy', 'cheese whiz'])}
/>
</main>
)
Expand Down Expand Up @@ -112,10 +122,13 @@ describe('HookedOnRedux', () => {

const list = getByTestId('list')
const addItems = getByTestId('add')
const replaceItems = getByTestId('replace')

expect(getNodeText(list)).toBe('milk,...')
fireEvent.click(addItems)
expect(getNodeText(list)).toBe('milk,eggs,bread,taters,...')
fireEvent.click(replaceItems)
expect(getNodeText(list)).toBe('cookies,candy,cheese whiz,...')
})
it('should predictably update objects', () => {
const reducer = createHookedOnReducer()
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import get from 'lodash/get'
import isString from 'lodash/isString'
import isArray from 'lodash/isArray'
import createReducer from 'redux-updeep'
import { constant } from 'updeep'
import { useSelector, useDispatch } from 'react-redux'

export const createHookedOnReducer = (initialState = {}, namespace = 'HOOKED_ON_REDUX', handlers = {}) =>
Expand All @@ -19,7 +18,7 @@ export const useHookedOnState = (selector, defaultState, options = {}) => {

const updateValue = newValue =>
dispatch({
payload: isArray(newValue) ? constant(newValue) : newValue,
payload: isArray(newValue) ? newValue.slice() : newValue,
path: selector,
type: `${namespace}/${selector}`
})
Expand Down
8 changes: 0 additions & 8 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 @@ -27,8 +27,7 @@
"homepage": "https://github.com/harryhope/hooked-on-redux#readme",
"dependencies": {
"lodash": "^4.17.15",
"redux-updeep": "^0.1.1",
"updeep": "^1.2.0"
"redux-updeep": "^0.1.1"
},
"peerDependencies": {
"react-redux": "^7.2.0"
Expand Down

0 comments on commit fab3090

Please sign in to comment.