Skip to content

Commit

Permalink
Memoized useActions
Browse files Browse the repository at this point in the history
* replace unnecessary `useRef` in delegatePage
  • Loading branch information
izri16 committed Apr 9, 2021
1 parent 6957d15 commit 9468513
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
13 changes: 4 additions & 9 deletions app/frontend/components/pages/delegations/delegatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,20 @@ const Delegate = ({withAccordion, title}: Props): h.JSX.Element => {
)
const handleOnStopTyping = useHandleOnStopTyping()

// TODO: this reference always changes inside "useEffect" which causes infite loop.
// Till this is investigated, this workaround is used instead of "eslint-disable-..."
const _updateStakePoolIdentifier = useRef(updateStakePoolIdentifier)
const _resetStakePoolIndentifier = useRef(resetStakePoolIndentifier)

const [fieldValue, setFieldValue] = useState('')
const [error, setError] = useState<Error>(null)

const handleInputValidation = useCallback(
(value: string) => {
if (!value) {
_resetStakePoolIndentifier.current()
resetStakePoolIndentifier()
setError(null)
} else {
const {poolHash, error} = validateInput(value, validStakepoolDataProvider)
if (!error) {
_updateStakePoolIdentifier.current(poolHash)
updateStakePoolIdentifier(poolHash)
} else {
_resetStakePoolIndentifier.current()
resetStakePoolIndentifier()
}
setError(error)
}
Expand All @@ -168,7 +163,7 @@ const Delegate = ({withAccordion, title}: Props): h.JSX.Element => {
using hybrid solution involving `updateStakePoolIdentifier` (should be later removed)
*/
},
[validStakepoolDataProvider]
[validStakepoolDataProvider, updateStakePoolIdentifier, resetStakePoolIndentifier]
)

const handleOnInput = (event: any): void => {
Expand Down
6 changes: 5 additions & 1 deletion app/frontend/helpers/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {mapActions} from '../libs/unistore/util'
// eslint-disable-next-line
import {State} from '../state'
import {ComponentClass, FunctionComponent} from 'preact'
import {useMemo} from 'preact/hooks'

// Note(ppershing): Enjoy generic insanity!

Expand Down Expand Up @@ -51,6 +52,9 @@ type UseSelector = <T>(selector: (state: State) => T) => T
const useSelector: UseSelector = _useSelector

type UseActions = <T>(actions: (store: any) => T) => BindActions<T>
const useActions: UseActions = (actions) => mapActions(actions, useStore())
const useActions: UseActions = (actions) => {
const store = useStore()
return useMemo(() => mapActions(actions, store), [actions, store])
}

export {connect, useStore, useSelector, useActions}

0 comments on commit 9468513

Please sign in to comment.