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

Open
yahyaensi opened this issue May 31, 2024 · 3 comments
Open

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

yahyaensi opened this issue May 31, 2024 · 3 comments
Labels
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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