Formik: setValues and values

Created on 26 Oct 2017  Â·  6Comments  Â·  Source: formium/formik

If I have props.values as below

const {
 values,
 setValues
} = props

const {
 names
} = values

where names equals to the following object

{
  user1: 'jon',
  user2: 'darrell'
}

how do I use setValues to change names.user1 to 'Louisa' for example?

Do I have to pass the entire mutated values object into setValues? IE:

values.names.user1 = 'Louisa'
setValues(values)

Most helpful comment

Can I do this then?
@6ewis @jaredpalmer

setFieldValue('names.user1', 'Louisa)

I eventually settled with the below initially.

values.names.user1 = 'Louisa'
setValues(values)

All 6 comments

@dorkblue How did you end up handling that case?

setFieldValue

in 0.11.0-alpha, you could use the new array path syntax for name="names.user1"

I noticed it. I was about to tell you the same :)

On Dec 5, 2017, at 3:39 PM, Jared Palmer notifications@github.com wrote:

setFieldValue

in 0.11.0-alpha, you could use the new array path syntax for name="names.user1"

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/jaredpalmer/formik/issues/220#issuecomment-349434176, or mute the thread https://github.com/notifications/unsubscribe-auth/AAzToa48GhUqYm5csAoUpM7aWd2j9uNrks5s9an6gaJpZM4QHAok.

Can I do this then?
@6ewis @jaredpalmer

setFieldValue('names.user1', 'Louisa)

I eventually settled with the below initially.

values.names.user1 = 'Louisa'
setValues(values)

This thread is old, but if you've gotten here late, one solution is using the immer library:

form.setValues(
    produce(form.values, draft => {
        draft.foo = "bar"
    })
)

This thread is old, but if you've gotten here late, one solution is using the immer library:

form.setValues(
    produce(form.values, draft => {
        draft.foo = "bar"
    })
)

just clone the object without a new library

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emartini picture emartini  Â·  3Comments

Jucesr picture Jucesr  Â·  3Comments

outaTiME picture outaTiME  Â·  3Comments

green-pickle picture green-pickle  Â·  3Comments

PeerHartmann picture PeerHartmann  Â·  3Comments