"different functions with the same name", but I'm using useCallback.
See original GitHub issueI’ve tried posting this on StackOverflow with no response was hoping to get some more detail here. I recreated a very simple project to replicate what’s going on in a bigger project of mine. https://github.com/DaleSalcedo/PropFunctionProblem. In my App.js I’m passing down this function:
const modifyPlayer = React.useCallback((playerId, propertyName, value) => {
const playerCopy = {...playerDict[playerId]};
playerCopy[propertyName] = value;
const playerDictCopy = {
...playerDict,
[playerId]: playerCopy
};
setPlayerDict(playerDictCopy); // useState function
}
,[playerDict]);
I pass that down 2 components as props and use it here:
export const Player = ({player, modifyPlayer}) => {
const handleOnChange = React.useCallback((event) => {
modifyPlayer(player.id, event.target.name, event.target.value);
}, [player, modifyPlayer]);
return (
<div>
<input type={"text"} name={"firstName"} value={player.firstName} onChange={handleOnChange}/>
<input type={"text"} name={"lastName"} value={player.lastName} onChange={handleOnChange}/>
</div>
);
};
I’m not sure why it’s giving me that message “different functions with the same name” for “modifyPlayer” function when I used “useCallback”.
Is this a bug or can someone explain what’s going on?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Your Guide to React.useCallback() - Dmitri Pavlutin
Functions in JavaScript are first-class citizens, meaning that a function is a regular object. The function object can be returned by other ......
Read more >Understanding useMemo and useCallback - Josh W Comeau
I'm not saying that one approach is better than the other; each tool has its spot in the toolbox. But in this specific...
Read more >All About React useCallback() - Hook API Reference In React
Let us first understand what useCallback is. useCallback is a hook that will return a memoized version of the callback function that only...
Read more >What's the difference between useCallback and useMemo
useCallback will memoize and return the actual function, so even though the function is memoized it will still be run whenever you call...
Read more >How and when to use React useCallback() - Amber Wilson
In short, React's useCallback hook is used to wrap functions. It tells React to not re-create a wrapped function when a component re-renders ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
fixed in v5.0.0-alpha.2
Re-opened for the record because I was asked about it again:
We wait on https://github.com/facebook/react/issues/14099 to be fixed so the fn returned from
useCallback
wan’t cause a re-render of the component it is passed to.