Calling setState from callback updates the state in React Context half the time

Here is the CodeSandbox:

I have implemented a modal with React context which exposes 1) the open state 2) the modal config 3) an open and close method

// ...

const onOpenDialog = (mode, config) => {
  if (config) {

const onCloseDialog = () => {
  if (Object.keys(dialogConfig).length > 0) {

// ...

return (
  <Provider value={{ dialogMode, dialogConfig, onOpenDialog, onCloseDialog }}>

From the main component, I have an onClick handler that call the onOpenDialog method and pass an onSubmit and onClose callback in its config object (this onCloseDialog callback is the issue)

const { onOpenDialog, onCloseDialog } = useDialog()

// ...

const onClick = () => {
  onOpenDialog("add", {
    data: null,
    onSubmit: (data) => {
      console.log("Form Data: ", data)
    onCancel: onCloseDialog

And finally, I have a FormInModal component that call the two callbacks passed in the dialogConfig object when hitting submit and close.

const onSubmit = (data) => {
    username: data.username,
    password: data.password

const onCancel = () => {

Steps to reproduce:

  1. Click on Open
  2. You should see in the console that 1) The dialog opens 2) The config input that was passed to the Dialog config has data, onSubmit and onCancel, the config input has been set in the dialogConfig state
  3. Click on Cancel, you should see that 1) The dialog closes 2) The dialogConfig state is empty 3) the dialogConfig state is not reseted

To be fair 3. works half of the time. This is weird because the dialogConfig state is always updated when the dialog opens. You can see on the React Dev tool that the state update only half the time

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum