How to update nested state properties in React

后端 未结 26 2040
野趣味
野趣味 2020-11-21 06:35

I\'m trying to organize my state by using nested property like this:

this.state = {
   someProperty: {
      flag:true
   }
}

But updating

26条回答
  •  失恋的感觉
    2020-11-21 07:26

    If you are using formik in your project it has some easy way to handle this stuff. Here is the most easiest way to do with formik.

    First set your initial values inside the formik initivalues attribute or in the react. state

    Here, the initial values is define in react state

       state = { 
         data: {
            fy: {
                active: "N"
            }
         }
       }
    

    define above initialValues for formik field inside formik initiValues attribute

     {...your actions goes here}}
    >
    {({ isSubmitting }) => (
      
    { const value = e.target.checked; if(value) setFieldValue('fy.active', 'Y') else setFieldValue('fy.active', 'N') }}/> )}

    Make a console to the check the state updated into string instead of booleanthe formik setFieldValue function to set the state or go with react debugger tool to see the changes iniside formik state values.

提交回复
热议问题