Skip to content

Commit 3279cf3

Browse files
committed
Add Update Formik Initial Values When Props Change as a react til
1 parent 5887c9a commit 3279cf3

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_828 TILs and counting..._
13+
_829 TILs and counting..._
1414

1515
---
1616

@@ -567,6 +567,7 @@ _828 TILs and counting..._
567567
- [Spelunking Through Components With Enzyme's Dive](react/spelunking-through-components-with-enzymes-dive.md)
568568
- [Sync Your react-router State With Redux](react/sync-your-react-router-state-with-redux.md)
569569
- [Test Files In create-react-app](react/test-files-in-create-react-app.md)
570+
- [Update Formik Initial Values When Props Change](react/update-formik-initial-values-when-props-change.md)
570571
- [Upgrading To The Latest React In CodeSandbox](react/upgrading-to-the-latest-react-in-codesandbox.md)
571572
- [Use A Ref To Autofocus An Input](react/use-a-ref-to-autofocus-an-input.md)
572573
- [Use React 16 With Gatsby](react/use-react-16-with-gatsby.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Update Formik Initial Values When Props Change
2+
3+
When a [Formik](https://jaredpalmer.com/formik/) form mounts, whatever the
4+
initial values are set to is what they will be. Even if the initial values are
5+
computed from props, those props changing will not affect `initialValues` after
6+
mount.
7+
8+
```javascript
9+
const ZipForm = ({ currentZip }) => {
10+
return (
11+
<Formik
12+
initialValues={{ zip: currentZip }}
13+
onSubmit={(values, actions) => {
14+
// do stuff
15+
}}
16+
...
17+
```
18+
19+
If we are fetching the user's saved zip code asynchronously from a server while
20+
the form is first being rendered, then `currentZip` will start as an empty
21+
value. Once the async request comes back and `currentZip` is set, we won't see
22+
the form update the `zip` field.
23+
24+
There was a time when you would have to jump through some hoops to make sure
25+
the freshest prop value made it into the form. Now, Formik provides a handier
26+
mechanism -- the `enableReinitialize` prop.
27+
28+
```javascript
29+
const ZipForm = ({ currentZip }) => {
30+
return (
31+
<Formik
32+
initialValues={{ zip: currentZip }
33+
enableReinitialize
34+
onSubmit={(values, actions) => {
35+
// do stuff
36+
}}
37+
...
38+
```
39+
40+
By setting `enableReinitialize` to true, we are telling Formik that any prop
41+
changes that flow into the `initialValues` object should cause those values to
42+
be _reinitialized_.
43+
44+
See a [live example](https://codesandbox.io/s/sad-mendeleev-4dbbp).

0 commit comments

Comments
 (0)