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.

return memoized value

See original GitHub issue

currently returned value both of useMutation and useQuery calculated every time they called, I’m not really familiar with apollo logics, but I guess this can memoized?, for instance useMutation should be similar like this:

export function useMutation(mutation, baseOptions) {
  const previousOptions = useRef();
  const client = useApolloClient();
  const result = isEqual(previousOptions.current, baseOptions);
  return useMemo(
    () => {
      previousOptions.current = baseOptions;
      return localOptions => client.mutate({
        mutation,
        ...baseOptions,
        ...localOptions,
      });
    },
    [
      mutation,
      result ? previousOptions.current : baseOptions,
    ],
  );
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
sijadcommented, Dec 19, 2018

no, I did not, but I guess with memorized version you can actually prevent re-rendering e.g:

const mutate = useMutation(MUTATION)
const memoizedMutation = useCallback(() => {
  mutate({
    variables,
  });
}, [mutate]);

return (<button onClick={memoizedMutation}>click on me</button>);

with current implimention mutate always have a new value therefore it might re-render more…

1reaction
avocadowastakencommented, Dec 27, 2018

Checking for mutation itself is not enough, we have to check for changes of options parameters, and this will create unnecessary memoization most of the times.

Anyways, React team does not suggest to pass individual callbacks in props and there is (not ideal) solution for this. I hope they will come up with better idea.

But if you don’t like it, you still can use your own hook for memoization , e.g:

function useMemoMutation(mutation, options, inputs) {
   return useCallback(useMutation(mutation, option), inputs);
}

function Foo({ bar, baz }) {
  const mutate = useMemoMutation(Mutate, {
    variables: { bar, baz }
  }, [bar, baz]) // Update only on `bar` or `baz` change
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Memoize Functions and Values in JavaScript and React
Unlike the useMemo() hook that memoizes only the returned value of a function, React.memo memoizes the whole function. Use React.memo only for ...
Read more >
Memoization in React js - Topcoder
In React, the memo is the higher-order component in short HOC (HOC are functions that take a component and return a new component)....
Read more >
What is Memoization? How and When to ... - freeCodeCamp
With memoization, there's no need to recalculate the same values once and again – we just store each computation and return the same...
Read more >
What is Memoization in React? | Syncfusion Blogs
Memoization is an optimization technique for accelerating computer programs by caching the results of heavy function calls and returning ...
Read more >
How to Implement Memoization in React to Improve ... - SitePoint
In this tutorial, we'll learn how to implement memoization in React. Memoization improves performance by storing the results of expensive ...
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