Form.create new API discussion: Render Props or React Hooks
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
What problem does this feature solve?
In order to avoid to wrap a component with Form.create(), it could be nicer to be able to define it in a declarative way.
What does the proposed API look like?
const SomeComponent = () => (
...
<Form.Create {...options}>
{({getFieldDecorator, ...}) => (
<Form onSubmit={...}>
{...}
</Form>
)}
</Form.Create>
...
)
Currently I use:
const FormCreate = Form.create()(
({ form, children }: { form: WrappedFormUtils; children(form: WrappedFormUtils): React.ReactElement<any> }) =>
children(form),
);
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:23 (13 by maintainers)
Top Results From Across the Web
Render Props vs React Hooks: Which Is More Efficient?
I started a twitter discussion with the same question, trying to understand if people have strong opinions about hooks and render props.
Read more >Render Props - React
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render...
Read more >Render Props vs React Hooks - Miroslav Nikolov
Hooks and render props can solve the same problem. It is conceptually about moving state away from your components, so that it is...
Read more >Exploring Render Props Vs. React Hooks In 2020 - HackerNoon
Unloading the Form component state-wise using the render props approach requires you to create a wrapper component. So, no hooks on the ...
Read more >React render props vs. custom Hooks - LogRocket Blog
Using a render prop can negate the advantage that comes from using PureComponent if we create a function assigned inside the render method....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
React hooks may be next API solution instead render props: https://github.com/ant-design/ant-design/issues/12857#issuecomment-433777455
from @yesmeck
It’s there any progress in this proposal? @afc163 Here is the way I use Ant Design
Form
component with React HooksMy API Proposal
useForm(initialState)
Create the form in the function components, it returns
FormObject
which including some sub hooks, they will be introduced later.Params
initialState
:{ [key: string]: string | number | string[] | number[] }
the initial state of the form.Returns
FormObject
: including form value, form item hooks and so on.FormObject.form: formStateObject
All form states are stored in
FormObject.form
:FormObject.form.formState
form item and it’s valueFormObject.form.touchedObject
form item touch stateFormObject.form.validityObject
form item touch validityFormObject.form.errorObject
form errorsExample
FormObject.useFormItem(formKey)
Proxy
<Form.Item>
props and auto set validate info or error status, likegetFieldDecorator
.Params
formKey
:string
form key which had defined ininitialState
.Returns
The same as props of
<Form.Item>
validateStatus
:'success' | 'warning' | 'error' | 'validating'
help
:string
FormObject.useValidator(validator[, deps])
Validate whole form state when form value changing.
Params
validator
:function (formState, setters): void
formState
:formStateObject
setters
:{ setValidityObject, setErrorObject }
deps
:any
when deps change, validator will be fired, default isformStateObject.formState
FormObject.input(formKey[, options])
FormObject.radio(formKey[, options])
FormObject.inputNumber(formKey[, options])
FormObject.textArea(formKey[, options])
FormObject.select(formKey[, options])
Hook Ant Design components
Params
formKey
:string
form key which had defined ininitialState
.options
options.validator
:function (value, setValidityObject): ErrorObject
validate this form itemvalue
:any
value offormKey
setValidityObject
:function () {}