Unexpected copies of the props object retained in memory with the new hooks API
See original GitHub issueDo 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:
- Created 5 years ago
- Reactions:4
- Comments:8 (3 by maintainers)
Top 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 >
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 Free
Top 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
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.
Does this still reproduce with React 18? We’ve made some memory-related changes there.