Skip to content Skip to sidebar Skip to footer

React+flux: Notify View/component That Action Has Failed?

I'm writing a Registration Form Component. When form submits it triggers a create user action. createUser actions creates a new user via an ajax api call. If a user already exist,

Solution 1:

Using Promises has helped me deal with asynchronous success/error handling. We can modify the createUser action to (at a later time) trigger success/failure actions depending on if the user was saved correctly or not:

// Actions
var UserActions = {
    userCreated : function(user) {
        Dispatcher.dispatch({
            actionType : ActionTypes.CREATE_USER,
            user : user
        });
    },

    createUserFailed : function() {
        Dispatcher.dispatch({
            actionType : ActionTypes.CREATE_USER_FAILED
        });
    },

    createUser : function(user) {
        // You should update the saveUser method to return a Promise that
        // resolves the new user on success and rejects on failure
        // (ie if the user already exists). Alternatively, you could update
        // this function to accept success/error callbacks.
        UserApi.saveUser(user)
            .then(this.userCreated.bind(this))
            .catch(this.createUserFailure.bind(this));
    }
};

Post a Comment for "React+flux: Notify View/component That Action Has Failed?"