Search code examples
angularreduxngrx

Why does add SUCCESS action in ngrx


I have noticed in ngrx tutorials that when writing actions they declare for example:

GET_MATIERES = '[matiereList] Get Matieres',
SUCCESS_GET_MATIERES = '[matiereList] Success Get Matieres',

Why don't use only

GET_MATIERES = '[matiereList] Get Matieres',

When Should we use SUCCESS action and when it's useless to do it ?


Solution

  • If you want to use effects you need to have success action and also having fail action as optional.

    so the flow will be like that

    1 - dispatch action
    
    2 - effect catch the action
    
    2.1 - effect calls http service
    
    2.2 - effect dispatch success action with data received from http 
    
    3 - reducer catch the success action and updates store
    

    If you don't want to use effects flow will be like below

    1 - call an angular service
    
    1.1 - angular service calls http
    
    1.2 - angular service dispatch action with data receives from http
    
    2 - reducer catch the action and updates store 
    

    Although using effects will cause more boiler plate code, but also it has the advantage of chaining the actions, for example after login you want to retrieve client info, or after save an invoice, you want to refresh list of invoices