Skip to content

Commit

Permalink
Improve README with initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-lb committed Jun 3, 2020
1 parent 0ed4741 commit 4e08b4d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,68 @@ submitForm({
});
```
## Usage - Custom form initial values
By default, formup will already create the form initial values according to your yup schema. This means that any `default(...)` will be taken into consideration, and it will always initialize the form as an empty valid object of your schema.
For example, the schema:
```js
const schema = yup.object().shape({
userId: yup
.number()
.default(100),
personalInformation: yup.object().shape({
firstName: yup
.string()
.required(),
email: yup
.string()
.default("[email protected]")
.required(),
}),
});
```
Will automatically translate to:
```js
{
userId: 100,
personalInformation: {
firstName: "",
email: "[email protected]",
},
}
```
However, you can customize any value generated within your initial values by passing `initialValues` to `useFormup` options. Formup will automatically merge the two objects into one, taking your overrides into consideration.
Here's an example, using the schema declared above:

```js
useFormup(schema, {
initialValues: {
userId: 999,
personalInformation: {
firstName: "Foo",
},
},
});
```

This will produce:

```js
{
userId: 999,
personalInformation: {
firstName: "Foo",
email: "[email protected]",
},
}
```

## Contributing

Pull requests are welcome!
Expand Down

0 comments on commit 4e08b4d

Please sign in to comment.