score:3

Accepted answer

one way to notify your component whether it was a success or a failure is to have your action return a promise. so your action would look like this:

export function addboard(data){
    return function(dispatch){
        return axios.post(`${api_url}/boards`,
            {
                name: data.name
            }
        )
        .then(response => {
            if(response.status === 201){
                const result = response.data.result;
                dispatch({ type: add_board_success, data: result });
                return result;
            }
        });
    }
}

then in your component you can do this:

addboard(formprops) {

    this.props.addboard(formprops).then((result) => {
        console.log("success!", result);
    })
    .catch(error => {
        console.error(error);
    });
}

you should probably also handle the catch in your action, if you want to dispatch a add_board_fail on an error, but then you'd have to rethrow that error if you still want to catch it in the component.


Related Query

More Query from same tag