What I want to achieve is this but using react admin SimpleForm
instead of Form
:
import React, { useState } from "react";
export function NameForm(props) {
const [name, setName] = useState("");
const handleSubmit = (evt) => {
evt.preventDefault();
alert(`Submitting Name ${name}`)
}
return (
<form onSubmit={handleSubmit}>
<label>
Frirst Name:
<input
type="text"
value={name}
onChange={e => setName(e.target.value)}
/>
</label>
<input type="submit" value="Submit" />
</form>
);
}
When I try the same pattern, i.e.:
<SimpleForm onSubmit={handleSubmit}>
it never reaches the handleSubmit function. I also tried:
<SimpleForm handleSubmit={handleSubmit}>
But again no joy.
The react admin docs here say:
Finally, it receives a handleSubmit function as prop, to be called with the updated record as an argument when the user submits the form.
Unfortunately being new to react this doesn't give me any clue as to what I should do to get this to work.
When you're using any third party library you need to follow their rules. Here you're using React admin library which support normal admin features like add/edit/listing etc. With minimal effort you can create admin panel.
So when you're focusing on creating form using React Admin , you can create add/edit form.
In your
App.js
you need to first define routing usingResource
which contains create and edit attribute. In create/edit you can import your add/edit component and pass it. The example is given below. You can seedataProvider
link is also provided. When you'll create edit form it will take data from thereAfter creating proper routing you can go to your component and can create add/edit form just like below
React-admin injects a few props to the create and edit views: the resource name, the basePath (the root URL), the permissions, and, in the case of the edit view, the record id. That’s why you need to pass the props to the
<Create>
and<Edit>
components.The
<Create>
and<Edit>
components call thedataProvider
, prepare the form submit handler, and render the page title and actions.<SimpleForm>
Which you mentioned in your question is responsible for creating the form only, it's not responsible forhandleSubmit
operation ,<Create>
and<Edit>
components handle that.To know more in details follow the React Admin
<Create>
and<Edit>
doc carefully.