Git Product home page Git Product logo

401d23-frontend-development's Introduction

cf 31: Redux

Learning Objectives

  • Students will learn to use redux with react
  • Students will learn to design redux reducer's for controlling application state
  • Students will learn to design action creator functions for working with redux

Readings

Outline

Redux

Redux is a state container for Javascript applications. It can be used to manage the state of any JS program, not just react apps. Redux holds the whole state of your application within a single store. The redux store becomes the "single source of truth" for all application state. Some developers go as far as to store all state (application state and view state) on the store. a Redux store is read only, much like a React component's state or props. Changes to a store are made with pure functions called reducers.

Reducers

A redux store is created by passing a function called a reducer into createStore. A redux reducer's job is to both define the state of the application and the changes that can be made to that state. Redux reducers have the function signature (state, action) => newState. The reducer will be called each time an action is dispatched and what ever state it returns will be the new state of the store.

Store

A redux store has three methods getState, dispatch, and subscribe

Dispatchers

Each time dispatch is invoked, it's first argument is passed into the reducers action parameter. In order to update the store, you must organize your reducer in a way that enables meaningful changes based on values that are dispatched. The most common pattern for dispatching meaningful changes to reducers is by always dispatching objects that have a type and payload. The reducer can then make decisions of what to do with the payload based on the type property of the action dispatched.

// example dispatch usage
store.dispatch({
  type: 'CREATE_NOTE',
  payload: {id: 'abc123', content: 'hello world'},
})

People often create helper functions called "action creators" that create action objects to be dispatched...

// an action creator for creating notes
const createNote = (note) => ({ type: 'CREATE_NOTE', payload: note })

dispatch(createNote({id: '123456', content: 'hello again, world'}))
getState

Invoking store.getState() returns the current state of the store.

subscribe

Subscribe allows you to register change listener functions that will be called each time the store is dispatched.

// log the new state after each dispatch
store.subscribe(() => {
  console.log('___STATE___', store.getState())
})

React Redux

react-redux is the official redux bindings for react. It is a set of tools that simplify creating react component's that are able to interact with a redux store.

Provider

Provider is a react component that makes the redux store available to connect() calls in the component hierarchy. You will wrap the provider around the rest of your application.

connect

The connect function connects a react component to the redux store added to your app's Provider component. connect uses mapStateToProps and mapDispatchToProps to create a higher order function used to wrap a react component.

mapStateToProps

mapStateToProps is a function that allows you to assign the store's state or parts of the store's state to props of a component.

mapDispatchToProps

mapDispatchToProps is a function that allows you to assign methods to a component's props that have access to the store's dispatch method.

401d23-frontend-development's People

Contributors

vladimirsan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.