I need people who have experience with React to review my code.
I want to make a debounce using Lodash.
import debounce from 'lodash/debounce'
const [name, setName] = React.useState<string | undefined>(undefined)
const [displayedName, setDisplayedName] = React.useState<string | undefined>(undefined)
const changeName = debounce((e: domProperty) => {
setName(e.target.value)
}, 1000)
React.useEffect(() => {
// call API
}, [name])
return (
<Input
placeholder="Player name"
prefix={<UserOutlined />}
value={displayedName}
onChange={(e) => {
changeName(e);
setDisplayedName(e.target.value);
}}
/>
)
Is it good? or is there anything wrong there?
So here I use displayedName because when I type something in Input, the name state doesn't change as it might be blocked by a debounce.
For example: when I type "alex" at the beginning (it works), then when I want to add the letter "i" (it doesnt, so I can't add or remove any letter it always "alex"), the name state is still the same as "alex". Therefore I add displayedName.