Skip to content

New hook/use set state with callback #33940

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iamdivypatel
Copy link

Summary

How did you test this change?

rmethaniya and others added 2 commits July 18, 2025 12:29
This commit introduces the useSetStateWithCallback hook across various React components, allowing state updates with an optional callback. The hook is implemented in the React reconciler and debug tools, with appropriate error handling for unsupported scenarios in server components. Additionally, the dispatcher types have been updated to include this new hook, ensuring compatibility with existing functionality.
…s React components, allowing state updates with an optional callback. The hook is implemented in the React reconciler and debug tools, with appropriate error handling for unsupported scenarios in server components. Additionally, the dispatcher types have been updated to include this new hook, ensuring compatibility with existing functionality.
Copy link

meta-cla bot commented Jul 18, 2025

Hi @iamdivypatel!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@iamdivypatel
Copy link
Author

📦 Contributing useSetStateWithCallback to React

🔧 Overview

This document describes the custom hook useSetStateWithCallback, designed to enhance state management in React functional components by allowing a callback after state updates, similar to the setState callback in class components.


🪝 Hook: useSetStateWithCallback

🧠 Purpose

Enables state updates with a post-update callback in functional components.


🧪 Usage Example

import { useSetStateWithCallback } from 'react';

function Demo() {
  const [open, setOpen] = useSetStateWithCallback(false);

  function handleOpen() {
    setOpen(true, () => {
      alert('Dialog is now open! Value: ' + open);
    });
  }

  function handleClose() {
    setOpen(false, () => {
      alert('Dialog is now closed! Value: ' + open);
    });
  }

  return (
    <>
      <p>State: {open ? 'Open' : 'Closed'}</p>
      <button onClick={handleOpen}>Open</button>
      <button onClick={handleClose}>Close</button>
    </>
  );
}

✅ Sample Test

import React, { useEffect } from 'react';
import { create } from 'react-test-renderer';
import { useSetStateWithCallback } from '../src/useSetStateWithCallback';

function TestComponent({ onReady }) {
  const [val, setVal] = useSetStateWithCallback(0);

  useEffect(() => {
    setVal(10, (finalVal) => {
      onReady(finalVal);
    });
  }, []);

  return <div>{val}</div>;
}

test('callback executes after state update', (done) => {
  create(
    <TestComponent
      onReady={(val) => {
        expect(val).toBe(10);
        done();
      }}
    />
  );
});

📎 Notes

  • Backward compatible with existing hooks
  • Useful for scenarios like modals, form feedback, async sequences
  • Not included in public export unless approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants