Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MERN Quick Start Guide

You're reading from   MERN Quick Start Guide Build web applications with MongoDB, Express.js, React, and Node

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781787281080
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Eddy Wilson Iriarte Koroliova Eddy Wilson Iriarte Koroliova
Author Profile Icon Eddy Wilson Iriarte Koroliova
Eddy Wilson Iriarte Koroliova
Arrow right icon
View More author details
Toc

Table of Contents (8) Chapters Close

Preface 1. Introduction to the MERN Stack FREE CHAPTER 2. Building a Web server with ExpressJS 3. Building a RESTful API 4. Real-Time Communication with Socket.IO and ExpressJS 5. Managing State with Redux 6. Building Web Applications with React 7. Other Books You May Enjoy

Binding action creators to the dispatch method

Actions creators are just functions that generate action objects which can later be used to dispatch actions using the dispatch method. Take for example the following code:

const TYPES = { 
    ADD_ITEM: 'ADD_ITEM', 
    REMOVE_ITEM: 'REMOVE_ITEM', 
} 
const actions = { 
    addItem: (name, description) => ({ 
        type: TYPES.ADD_ITEM, 
        payload: { name, description }, 
    }), 
    removeItem: (id) => ({ 
        type: TYPES.REMOVE_ITEM, 
        payload: { id }, 
    }) 
} 
module.exports = actions 

Later, somewhere in your application, you can dispatch these actions using the dispatch method:

dispatch(actions.addItem('Little Box', 'Cats')) 
dispatch(actions.removeItem(123)) 

However, as you can see, calling the dispatch method every time seems like a repeated and unnecessary...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image