Why is state not showing up in the Modalize onOpened event?

158 views Asked by At

I have what seems to be a simple setup.

  • Some state the that holds the selected task.
  • A useEffect that opens the modal when the state is updated.

The modal opens, and inside the TaskScreen I'm able to console log selectedTask.

The problem is the onOpened prop never has the selectedTask. It always logs undefined.

How can TaskScreen have the correct value but not the onOpened prop? They're both rendered at the same time.

const taskModalRef = useRef(undefined);
const [selectedTask, setSelectedTask] = useState(undefined);

useEffect(() => {
  if (selectedTask) {
    taskModalRef.current.open();
  }
})

<Modalize
  ref={taskModalRef}
  onOpened={() => {
    console.log('selectedTask', selectedTask); //  always undefined.
  }}
>
  <TaskScreen task={selectedTask} /> //  always has the correct value.
</Modalize>

0

There are 0 answers