×

Html & Html5

The World's best Software Development Study Portal

Redux Actions


Actions are the only source of information for the store as per Redux official documentation. It carries a payload of information from your application to store. As discussed earlier, actions are plain JavaScript object that must have a type attribute to indicate the type of action performed.


  • Actions are the only way by which your application can interact with the store.

  • Redux Action carry some information from your app to the redux store.

  • We also learn,Actions are the plain javascript objects.

  • They must however have a 'type' property that indicates the type of action being performed.

  • the 'type' property is typically defined as string constants.



  • Example:

        index.js
    	
    	const BUY_CAKE = 'BUY_CAKE'
    	
    	function buyCake(){
    	    return{
    		type : BUY_CAKE,
    		info: 'First redux action'
    		
    		}
    		
    	}
    		
    	

    In the above example,we have created our action for the cake shop application.

  • First,we define a string constant that indicates the type of the action i.e,'BUY_CAKE'.

  • During this,we will avoid the spelling mistake while using the action.

  • Next,we define our action.Remember an action is an object that has a type property.

  • And like this, we have just created our first Action in redux.



  • Action Creator:


    As the name indicates,an action Creator simply creates an action.In terms of code,an action creator is a function that returns an action.So,to implement the action creator,we create a function which returns the action.

    In the above example,function buyCake() acts as an action creator.