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

the onSuccess property does not work in the Edit and Create component #5785

Closed
baselga opened this issue Jan 15, 2021 · 9 comments
Closed

the onSuccess property does not work in the Edit and Create component #5785

baselga opened this issue Jan 15, 2021 · 9 comments

Comments

@baselga
Copy link

baselga commented Jan 15, 2021

When the onSuccess property is added to the Edit or Create component, the SaveButton component stops working.

Environment

  • React-admin version: 3.11.2
  • Last version that did not exhibit the issue (if applicable): 3.10.4
  • React version: 17.0.1
  • Browser:
  • Stack trace (in case of a JS error):
@djhi
Copy link
Collaborator

djhi commented Jan 15, 2021

Please follow the issue template

@Dragonqos
Copy link

I'm observing the same issue.
When using "onSuccess" prop in "Edit/Create", after Submit no data sending to dataProvider anymore, instead of this got empty response object inside onSuccess func, and obviously component stops working.

Sandbox: https://codesandbox.io/s/laughing-bardeen-mwq4s?file=/src/posts/Edit.js

@baselga
Copy link
Author

baselga commented Jan 15, 2021

What you were expecting:
I hope you will call the onSusscess property after saving the form.

What happened instead:
When the onSuccess property is added to the Edit or Create component, the SaveButton component stops working.

Related code:

import React from 'react'
import { Edit, SimpleForm } from "react-admin"
import Form from './form'

export default (props) => (
  <Edit {...props} onSuccess={()=>console.log("onSuccess")}>
    <SimpleForm>
      <Form />
    </SimpleForm>
  </Edit>
)

Environment

  • React-admin version: 3.11.2
  • Last version that did not exhibit the issue (if applicable):3.10.4
  • React version:17.01
  • Browser: All
  • Stack trace (in case of a JS error):

@fzaninotto
Copy link
Member

The Edit view is undoable by default. Undoable save is triggered when the success notification disappears. In your example, the onSuccess side effect doesn't include a call to notify, therefore the notification never shows up, and the call to dataProvider.update() is never triggered.

This is a known gotcha of the optimistic / undoable mode (it relies on the trigger of a notification), but not a bug.

You can fix it in your code by adding a call to notify in your onSuccess callback, with the 4th parameter (undoable) true.

import React from 'react'
-import { Edit, SimpleForm } from "react-admin"
+import { Edit, SimpleForm, useNotify } from "react-admin"
import Form from './form'

export default (props) => {
+ const notify = useNotify();
  const onSuccess = () => {
    console.log("onSuccess");
+   notify('success', 'info', null, true);
  }
  return (
    <Edit {...props} onSuccess={onSuccess}>
      <SimpleForm>
        <Form />
      </SimpleForm>
    </Edit>
  );
}

You can also use <Edit undoable={false}> .

That being said, I agree that the documentation at https://marmelab.com/react-admin/CreateEdit.html#onsuccess isn't clear enough about this point. I'm turning this into a documentation issue.

@baselga
Copy link
Author

baselga commented Jan 17, 2021

Ok! Thank you for your support

@wmwart
Copy link

wmwart commented Jan 20, 2021

There is another problem. ({data}) always undefined

The onSuccess function receives the response from the dataProvider call (dataProvider.create() or dataProvider.update()), which is the created/edited record (see the dataProvider documentation for details)

React-admin version: 3.11.3

@wmwart
Copy link

wmwart commented Jan 20, 2021

leave here or create a new issue?

@WiXSL
Copy link
Contributor

WiXSL commented Apr 12, 2021

I believe this doc enhancement has been made with the new mutationMode prop.
So this issue can be closed.

@fzaninotto
Copy link
Member

Fixed by #5799

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

No branches or pull requests

6 participants