question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"different functions with the same name", but I'm using useCallback.

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
vzaidmancommented, Sep 1, 2020

fixed in v5.0.0-alpha.2

1reaction
vzaidmancommented, Jan 28, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found