Skip to content

Commit

Permalink
Refactor: debounce with useRef instead of useCallback
Browse files Browse the repository at this point in the history
"React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead."
  • Loading branch information
kevinashworth committed Dec 25, 2020
1 parent ddc5b9b commit 6dd4807
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
/**
* Specialized 3-part Select for contactId (props.value), contactName, contactTitle
*
* NB:
*/
import React, { useCallback, useState } from 'react'
import React, { useRef, useState } from 'react'
import PropTypes from 'prop-types'
import find from 'lodash/find'
import Form from 'react-bootstrap/Form'
Expand Down Expand Up @@ -37,14 +39,12 @@ const SelectContactIdNameTitle = (props, context) => {
setContactName(selectedOption.label)
}

// use debounce when both keyboard input and updateCurrentValues
// (but not other occurrences of updateCurrentValues)
const updateCurrentValues = (value) => {
// use debounce because both keyboard input and updateCurrentValues
const updateCurrentValuesDebounced = useRef(_debounce((value) => {
context.updateCurrentValues({
[pathPrefix + 'contactName']: value
})
}
const updateCurrentValuesDebounced = useCallback(_debounce(updateCurrentValues, 250), [])
}, 250), []).current
const handleNameChange = ({ currentTarget: { value } }) => {
setContactName(value)
updateCurrentValuesDebounced(value)
Expand Down

0 comments on commit 6dd4807

Please sign in to comment.