88
99* [ Why not Redux-Form or Formik?] ( #why-not-redux-form-or-formik )
1010* [ Why no HOC?] ( #why-no-hoc )
11+ * [ How can I trigger a submit from outside my form?] ( #how-can-i-trigger-a-submit-from-outside-my-form )
12+ * [ Via ` document.getElementById() ` ] ( #via-documentgetelementbyid )
13+ * [ Via Closure] ( #via-closure )
14+ * [ Via Redux Dead Drop] ( #via-redux-dead-drop )
1115
1216<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1317
@@ -21,8 +25,8 @@ depends on your requirements and what trade-offs you wish to make. Here is a
2125
2226The only benefit that higher order components provide over render props is
2327access to the injected props from within component lifecycle methods. Plus, it
24- only takes a single line of code to transform a component with a ` render `
25- (or ` component ` ) prop into a HOC. If you really want a HOC, you can write your own:
28+ only takes a single line of code to transform a component with a ` render ` (or
29+ ` component ` ) prop into a HOC. If you really want a HOC, you can write your own:
2630
2731``` jsx
2832import { Form , Field } from ' react-final-form'
@@ -46,3 +50,60 @@ Doing a HOC
4650[ properly] ( https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/withRouter.js ) ,
4751as a library should, with hoisted statics and ` displayName ` and ` ref ` , etc., is
4852a hassle and would add unnecessary bulk.
53+
54+ ## How can I trigger a submit from outside my form?
55+
56+ This is a common question I see from people migrating from ` redux-form ` . There
57+ are three possible solutions:
58+
59+ ### Via ` document.getElementById() `
60+
61+ You can use the DOM to get a reference to your ` <form> ` element and dispatch a
62+ submit event on it. Note that you cannot just call ` submit() ` , as this will not
63+ trigger React's event handlers.
64+
65+ ``` jsx
66+ < button onClick= {() => {
67+ document .getElementById (' myForm' ).submit () // ❌
68+ }}> Submit< / button>
69+
70+ < button onClick= {() => {
71+ document .getElementById (' myForm' ).dispatchEvent (new Event (' submit' )) // ✅
72+ }}> Submit< / button>
73+
74+ < form id= " myForm" onSubmit= {handleSubmit}>
75+ ... fields go here...
76+ < / form>
77+ ```
78+
79+ See [ Sandbox Example] ( https://codesandbox.io/s/1y7noyrlmq ) .
80+
81+ ### Via Closure
82+
83+ If you define a variable outside of your form, you can then set the value of
84+ that variable to the ` handleSubmit ` function that 🏁 React Final Form gives you,
85+ and then you can call that function from outside of the form.
86+
87+ ``` jsx
88+ let submit
89+ return (
90+ < div>
91+ < button onClick= {submit}> Submit< / button> // ❌ Not overwritten closure value
92+ < button onClick= {event => submit (event )}> Submit< / button> // ✅
93+ < Form onSubmit= {onSubmit} render= {({ handleSubmit })=> {
94+ submit = handleSubmit
95+ return < form>
96+ ... fields go here...
97+ < / form>
98+ }}>
99+ < / div>
100+ )
101+ ```
102+
103+ See [ Sandbox Example] ( https://codesandbox.io/s/1y7noyrlmq ) .
104+
105+ ### Via Redux Dead Drop
106+
107+ If you're already using Redux, you could potentially use the same mechanism that
108+ ` redux-form ` uses:
109+ [ Redux Dead Drop] ( https://medium.com/@erikras/redux-dead-drop-1b9573705bec ) .
0 commit comments