useCallback with a parameter solution

Viewed 24

I have some kind of dropdown controlled component, something like that:

const [dropdownValue, setDropdownValue] = useState("");
<Dropdown value={dropdownValue} onChange={value => setDropdownValue(value)}/>

(I want the onChange method to do more than just setting the value)

I thought about putting the onChange method inside useCallback but it seems the inner method will have to get a parameter.

I came up with this solution:

const [dropdownValue, setDropdownValue] = useState(someDefaultValue);
const onChange = useMemo(() => value => setDropdownValue(value)},[setDropdownValue]);
<Dropdown value={dropdownValue} onChange={onChange}/>

I think it will work fine because the method will be memoized. But i didn't see it before so i'm not sure if it is a valid solution to the problem, would like to hear opinions about that,

Thanks in advance :)

0 Answers
Related