1

I'm making a react flux app where the user first create a user and that user will be saved in a database.

so first I create a action named CREATE_USER looks like this

export function createUser(name, username, password) {
  dispatcher.dispatch({
    type: "CREATE_USER",
    name,
    username,
    password,
  });
}

then i register that action in my Store like this

handleActions(action) {
    switch(action.type) {
     case "CREATE_USER": {
      this.createUser(action.name, action.username, action.password);
      break;
     }
    }
  }

this will trigger a create user function that will make a http post to the backend and save that user in the database

createUser(name, username, password) {
  axios.post('/api/user', {
    name: name,
    username: username,
    password: password
  })
  .then(function (response) {
    console.log(response);

  })
  .catch(function (error) {
    console.log(error.response);

  });

 }

then in my component i call createUser on handlesubmit function

_handleUserSubmit(event) {
    event.preventDefault();
     let name = this.name.value;
     let username = this.username.value;
     let password = this.password.value;

     UserActions.createUser(name, username, password);
}

But how do i return the response object or error object from the createuser function to the component?

  • The store needs to be updated by the dispatcher by passing a callback to createUser() – vijayst Jul 27 '16 at 10:41
  • Do you have an example? –  Jul 30 '16 at 10:43
  • I don't have an example for flux. There is one for redux. https://github.com/vijayst/react-es6-starter. The concepts are similar. – vijayst Jul 30 '16 at 10:51
  • Ok another question should the Ajax post be made in actions or in the store? –  Jul 30 '16 at 16:07
  • Can be done in either action or the store. Check this related SO question: http://stackoverflow.com/questions/26632415/where-should-ajax-request-be-made-in-flux-app – vijayst Jul 31 '16 at 02:16

0 Answers0