javascript - Axios post method React. State Redux not update -
i have problem state
in redux
axios
request
case 'register_user':{ var userdata = {username: action.payload.username, password : action.payload.password}; axios({ method: 'post', url: 'php/register.php', contenttype: "application/json; charset=utf-8", datatype: "json", data: userdata }).then(function(response) { state.error = response.data.errors; state.success = response.data.success; }); return {state}; break; }//end register_user
default state.error
, state.success
empty array
. after first click state.error
still empty array because axois didn't finish request. after 2nd click fine.
and here have problem props:
{this.props.error.length>0? <div classname="alert alert-danger">{this.props.error[0]}</div>: ''}
and store:
@connect((store)=>{ return{ error: store.app.error, success: store.app.success, } })
do u know how can render component
new props
after axios method post
finished.
i think have written code reducer, api call should done inside action dispacher. after api called have dispatch data reducer. in reducer should assign data, no api calls inside reducer.
for e.g.
action dispatcher
export function apidispatch(data) { return{ type:'api_data_recd', data:data } } export function callapi(){ return function(dispatch){ axios({ method: 'post', url: 'php/register.php', contenttype: "application/json; charset=utf-8", datatype: "json", data: userdata }).then(function(response) { state.error = response.data.errors; state.success = response.data.success; dispatch(apidispatch(state)) }); } }
reducer
case 'api_data_recd': { // data action.data }
Comments
Post a Comment