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.

Unexpected copies of the props object retained in memory with the new hooks API

See original GitHub issue

Do you want to request a feature or report a bug?

bug

What is the current behavior?

function useCustomHook1() {
  useEffect(() => {
    console.log("mounted");
    return () => {
      console.log("unmounted");
    };
  }, []);
}

function Test3(props) {
  useCustomHook1();
  useEffect(() => {
    console.log(props.test);
  });

  return <div onClick={() => console.log(props.test)}>{props.items.length}</div>;
}

props object that were used when component was rendered for the first time is kept alive. Completely unexpected behavior even when developer understands closure context sharing.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://codesandbox.io/s/lz61v39r7

  • Select test 3
  • Click on the button “Create New Array” 5 times
  • Take memory snapshot in the developer tools
  • Inspect (array) objects
  • There will be 4 arrays retained in memory t0, t-2, t-1, t

What is the expected behavior?

Should be 2 arrays retained in memory t-1, t.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
stale[bot]commented, Jan 10, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

0reactions
gaearoncommented, Mar 29, 2022

Does this still reproduce with React 18? We’ve made some memory-related changes there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React.useState does not reload state from props
When setting initial state in constructor its clear to understand that constructor is called once on class instance creation. With hooks it ...
Read more >
How To Manage State with Hooks on React Components
React Hooks are a broad set of tools in the React front-end JavaScript library that run custom functions when a component's props change....
Read more >
Hooks API Reference - React
This page describes the APIs for the built-in Hooks in React. If you're new to Hooks, you might want to check out the...
Read more >
Accessing previous props or state with React Hooks
Let's explore in clear terms what's happening within the custom Hook and how the previous value is retained. useRef in depth: An overlooked ......
Read more >
When Using useMemo is A Really Bad Idea | by Lina Suodyte
The hook takes fetchListOfMovies function as a parameter and an array of ... In some cases useMemo might not retain our data in...
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