zetoune
zetoune

Reputation: 35

react-admin: `data` is undefined in onSuccess() on <Edit> component

I'm trying to follow the example from https://marmelab.com/react-admin/CreateEdit.html#onsuccess. I'd like to notify users with the record title on update.

    const onSuccess = ({ data }) => {
        console.log(data) // output: undefined
        notify(`Changes to post "${data.title}" saved`)
        redirect('/posts');
        refresh();
    };

JS always complains that undefined does not have a title property.

According to the docs, data comes from the response of dataProvider.update()

The onSuccess function receives the response from the dataProvider call (dataProvider.create() or dataProvider.update())...

But, from what I tested, onSuccess() is called right away on submit, and dataProvider.update() is called when the toast message disappears, so onSuccess() does not receive the result of the Promise returned by dataProvider.update().

Is there something I'm missing in the example?

Additional informations

I'm using react-admin v3.11.1 and Chrome 87.0.4280.88 The record is updated correctly if I remove onSuccess() from the Component

Upvotes: 0

Views: 992

Answers (1)

Striped
Striped

Reputation: 2547

If you are in Edit context you have to set undoable={false} to the Edit component to prevent optimistic response that does not wait for the request response.

<Edit
  {...props}
  onSuccess={onSuccess}
  undoable={false}
>
    ...
</Edit>

Ref: https://marmelab.com/react-admin/CreateEdit.html#undoable

Upvotes: 1

Related Questions