reactjs - Dynamically set form field values in React + Redux -
my app's store has store.authstate
subtree. in subtree, there 3 things, authtoken
, isfetching
boolean and, importantly fields
object. form contains 2 fields : username
, password
.
i have created action called set_form_field_value
should generate , update each field's state changed user.
i set_form_field_value
update fields
object. if user fills in both username
, password
fields, store.authstate
should this:
{ authtoken: undefined, isfetching: false, fields: { username: "brachamul", password: "azerty123" } }
however, seems current code overwrites , end :
{ field: { username: "brachamul" } }
the data in field changes based on field last edited. it's either username or password.
here code :
switch (action.type) { case 'set_form_field_value': let field = {} // create "field" object represent current field field[action.fieldname] = action.fieldvalue // give name , value of current field return { ...state.fields, field }
how can change fix issue ?
your return wrong, should this
switch (action.type) { case 'set_form_field_value': return { ...state, fields: { ...state.fields, [action.fieldname] : action.fieldvalue } } }
hope helps.
Comments
Post a Comment