React+Flux: Notify View/Component that action has failed?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 20:54:38

问题


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, triggered user action fails. We know we cannot return a response from an ajax call, we need a callback for that. How do i tell me component that a user already exists?

// Register From Component
var Register    =   React.createClass({
    saveUser : function(event){
        event.preventDefault();
        userActions.createUser(this.state.user);
    },

    render : function(){
        return (
            <div className="register-form">
                {message}
                <RegisterForm
                    user={this.state.user}
                    onChange={this.saveUserState}
                    onSave={this.saveUser}
                    errors={this.state.errors} />
            </div>
        );
    }
});

// Api Ajax call
module.exports = {
    saveUser : function(user) {
        $.ajax({
            type : 'POST',
            data : {
                email : user.email,
                password : user.password
            },
            success: function(res){

            }
        });
    },
};

// Actions
var UserActions = {
    createUser : function(user){
        var newUser = UserApi.saveUser(user);

        Dispatcher.dispatch({
            actionType : ActionTypes.CREATE_USER,
            user : newUser
        });
    }
};

I was thinking of returning a boolean value from createUser method.

...
onSave : function(event){

    if(userActions.createUser(this.state.user)){
         // 
    }
}
...

But we cannot return a value from ajax request, how can i notify my component that api has failed to create a new user, so that it can render a error message to user.


回答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));
    }
};


来源:https://stackoverflow.com/questions/35945788/reactflux-notify-view-component-that-action-has-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!