-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
usememo-overdose #37
Comments
I've created ESLint plugin to detect unnecessary useMemo https://github.com/shashkovdanil/eslint-plugin-usememo-recommendations |
what about if a memo is used to create a reference object and pass it as a prop? what about politics - avoid unnecessary rendering rather than doing a simple calculation? |
Absolutely wrong way to test performance of the memo: const startTime = performance.now();
const result = expensiveCalculation;
const endTime = performance.now(); |
@shashkovdanil Nice work out there! |
@sergeysova Hey, can you please elaborate on this? Why it's wrong? |
Yup, I like this idea. I assume we are speaking about something like this idea. import React, { useState, useMemo } from 'react';
// ChildComponent receives a reference object as a prop
const ChildComponent = ({ data }) => {
console.log('ChildComponent rendering');
// Use the data from the prop
return <div>Data: {data.value}</div>;
};
// ParentComponent creates a reference object and passes it to ChildComponent
const ParentComponent = () => {
const [count, setCount] = useState(0);
const referenceObject = useMemo(() => {
return { value: count };
}, [count]);
const handleClick = () => {
setCount(count + 1);
};
return (
<div>
<button onClick={handleClick}>Increment Count</button>
<ChildComponent data={referenceObject} />
</div>
);
};
export default ParentComponent;
I stay away from politics in general and bring systems instead. So, if you can agree within a team on what approach to use - stick to it. |
@ummahusla I guess in this sample |
@ummahusla if I'm reading this correctly, this code:
Is measuring the time that JS takes to create a new reference in memory and assign it the value of another reference. It does not call any actual function that would do any work worth measuring. |
@ummahusla another use-case I found myself using Would you still recommend useMemo for this, or do you have another pattern? |
Hey, thanks for your reply. I think I see where you coming form. I'm playing around with this component right now and will update the blog post at some point: import React, { useState, useMemo } from "react";
const MemoComponent = () => {
const [count, setCount] = useState(0);
const expensiveCalculation = useMemo(() => {
const startTime = performance.now();
// Simulate an expensive calculation
let result = 0;
for (let i = 0; i < 10000000; i++) {
result += i;
}
const endTime = performance.now();
console.log("Expensive calculation execution time:", endTime - startTime);
return result;
}, []); // Include 'count' as a dependency if you want to recompute when it changes.
const handleClick = () => {
setCount(count + 1);
};
return (
<div>
<button onClick={handleClick}>Increment Count</button>
<p>Count: {count}</p>
<p>Result: {expensiveCalculation}</p>
</div>
);
};
export default MemoComponent; |
@amiiit My blog post was published on the One link in particular I found interesting - https://attardi.org/why-we-memo-all-the-things/, I even emailed its author, to figure out whenever anything changed. This is his response:
|
useMemo overdose | Edvins Antonovs
When overuse of useMemo hook becomes a problem
https://edvins.io/usememo-overdose
The text was updated successfully, but these errors were encountered: