Skip to content

Commit

Permalink
Performance issue with useUpdater
Browse files Browse the repository at this point in the history
Generate actions and reducers once instead of every render
  • Loading branch information
doniyor2109 committed Dec 26, 2018
1 parent 405d9c2 commit 88a4a34
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/useUpdater.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useReducer } from 'react'
import { useReducer, useMemo } from 'react'

import { createUpdater } from './createUpdater';
import { bindActionCreators } from './helpers';

export function useUpdater(Updater) {
const [reducer, actions, initialState] = createUpdater(Updater)
const [reducer, actions, initialState] = useMemo(() => {
return createUpdater(Updater)
}, [])
const [state, dispatch] = useReducer(reducer, initialState)
return [state, bindActionCreators(actions, dispatch)]
}
3 changes: 2 additions & 1 deletion lib/useUpdater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ jest.mock('react', () => ({
useReducer: jest.fn((reducer, initialState) => ([
initialState,
jest.fn()
]))
])),
useMemo: jest.fn((callback) => (callback()))
}))

describe('useUpdater', () => {
Expand Down

1 comment on commit 88a4a34

@doniyor2109
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes #1

Please sign in to comment.