Skip to content
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

Bug: useCallback doesn't work sometimes inside memo component #29696

Closed
yahyaensi opened this issue May 31, 2024 · 5 comments
Closed

Bug: useCallback doesn't work sometimes inside memo component #29696

yahyaensi opened this issue May 31, 2024 · 5 comments
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@yahyaensi
Copy link

yahyaensi commented May 31, 2024

React: 18.3.1

In the following snippet (taken from React documentation), the console.log prints always false because each time the component rerendered, useCallback returns a new function reference: (only when the component is nested inside ProductPage component, but if I put ShippingForm directly under App component useCallback works fine)

let lastFakeFn = null;

const ShippingForm = memo(({ onSubmit }) => {
    const [count, setCount] = useState(1);

    const fakeFn = useCallback(() => {

    }, []);
    console.log(lastFakeFn === fakeFn)
    lastFakeFn = fakeFn;

    return (
            <label>
                Number of items:
                <button type="button" onClick={() => setCount(count - 1)}>–</button>
                {count}
                <button type="button" onClick={() => setCount(count + 1)}>+</button>
            </label>
    );
});
@yahyaensi yahyaensi added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label May 31, 2024
@yahyaensi yahyaensi changed the title Bug: useCallback doesn't work inside memo component Bug: useCallback doesn't work sometimes inside memo component May 31, 2024
@yahyaensi
Copy link
Author

I identified the problem, it's <React.StrictMode>

@testhowtest
Copy link

This issue is similar to #17783, #17221, and #15774. They might share similarities in causing unnecessary re-renders or failing to effectively memoize callback functions, but each problem seems to represent a unique scenario or manifestation of the issue. Is that correct?

@yahyaensi
Copy link
Author

Yes I think so

Copy link

github-actions bot commented Sep 2, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Sep 2, 2024
Copy link

github-actions bot commented Sep 9, 2024

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants