FMCalisto / redux-get-started

Redux Getting Started

Home Page:http://redux.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redux is a predictable state container for JavaScript apps.

It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.

You can use Redux together with React, or with any other view library.
It is tiny (2kB, including dependencies).

build status npm version npm downloads redux channel on discord #rackt on freenode Changelog #187

New! Learn Redux from its creator:
Getting Started with Redux (30 free videos)

Goals

This repository is just a Getting Started to learn Redux.

This is a COPY from the main redux source with some files made by me on the src.

Where to start?

You can start by watching the Getting Started with Redux tutorial (free), here Dan Abramov will help you learn redux while guiding you through the process of building apps.

Topics include:

  • The Single Immutable State Tree

  • Describing State Changes with Actions

  • Pure and Impure Functions

  • The Reducer Function

  • Writing a Counter Reducer with Tests

  • Store Methods: getState(), dispatch(), and subscribe()

  • Implementing Store from Scratch

  • React Counter Example

  • Avoiding Array Mutations with concat(), slice(), and ...spread

  • Avoiding Object Mutations with Object.assign() and ...spread

  • Writing a Todo List Reducer (Adding a Todo)

  • Writing a Todo List Reducer (Toggling a Todo)

  • Reducer Composition with Arrays

  • Reducer Composition with Objects

  • Reducer Composition with combineReducers()

  • Implementing combineReducers() from Scratch

  • React Todo List Example (Adding a Todo)

  • React Todo List Example (Toggling a Todo)

  • React Todo List Example (Filtering Todos)

  • Extracting Presentational Components (Todo, TodoList)

  • Extracting Presentational Components (AddTodo, Footer, FilterLink)

  • Extracting Container Components (FilterLink)

  • Extracting Container Components (VisibleTodoList, AddTodo)

  • Passing the Store Down Explicitly via Props

  • Passing the Store Down Implicitly via Context

  • Passing the Store Down with from React Redux

  • Generating Containers with connect() from React Redux (VisibleTodoList)

  • Generating Containers with connect() from React Redux (AddTodo)

  • Generating Containers with connect() from React Redux (FooterLink)

  • Extracting Action Creators

The Egghead.io Redux Course Notes repo contains notes from Dan Abramov's excellent Redux video series.

These notes contain a lot of verbatim transcriptions, along with additional rewrites, links, etc. that have been added along the way. Feel free to submit additions to these notes, but please don't remove anything (unless we messed up or misunderstood something).

Some of these documents contain multiple sections, but the majority are "one doc per vid" (there are 30 videos covered in 25 pages). Each section contains a link to the video, and towards the end of the series, timestamped links to Dan's "what we just did" recaps at the end of each lesson have been added.

For a working final product of these lessons, visit @sadams' todo-redux-react-webpack repo.

Feel free to submit a PR!

Gitbook

npm i -g gitbook-cli
npm install
gitbook install
gitbook serve

Installation

To install the stable version:

npm install --save redux

This assumes you are using npm as your package manager.
If you don't, you can access these files on npmcdn, download them, or point your package manager to them.

Most commonly people consume Redux as a collection of CommonJS modules. These modules are what you get when you import redux in a Webpack, Browserify, or a Node environment. If you like to live on the edge and use Rollup, we support that as well.

If you don't use a module bundler, it's also fine. The redux npm package includes precompiled production and development UMD builds in the dist folder. They can be used directly without a bundler and are thus compatible with many popular JavaScript module loaders and environments. For example, you can drop a UMD build as a <script> tag on the page, or tell Bower to install it. The UMD builds make Redux available as a window.Redux global variable.

The Redux source code is written in ES2015 but we precompile both CommonJS and UMD builds to ES5 so they work in any modern browser. You don't need to use Babel or a module bundler to get started with Redux.

Complementary Packages

Most likely, you'll also need the React bindings and the developer tools.

npm install --save react-redux
npm install --save-dev redux-devtools

Note that unlike Redux itself, many packages in the Redux ecosystem don't provide UMD builds, so we recommend using CommonJS module bundlers like Webpack and Browserify for the most comfortable development experience.

The Gist

The whole state of your app is stored in an object tree inside a single store.
The only way to change the state tree is to emit an action, an object describing what happened.
To specify how the actions transform the state tree, you write pure reducers.

That's it!

import { createStore } from 'redux'

/**
 * This is a reducer, a pure function with (state, action) => state signature.
 * It describes how an action transforms the state into the next state.
 *
 * The shape of the state is up to you: it can be a primitive, an array, an object,
 * or even an Immutable.js data structure. The only important part is that you should
 * not mutate the state object, but return a new object if the state changes.
 *
 * In this example, we use a `switch` statement and strings, but you can use a helper that
 * follows a different convention (such as function maps) if it makes sense for your
 * project.
 */
function counter(state = 0, action) {
  switch (action.type) {
  case 'INCREMENT':
    return state + 1
  case 'DECREMENT':
    return state - 1
  default:
    return state
  }
}

// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
let store = createStore(counter)

// You can use subscribe() to update the UI in response to state changes.
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
// However it can also be handy to persist the current state in the localStorage.

store.subscribe(() =>
  console.log(store.getState())
)

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
store.dispatch({ type: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1

Instead of mutating the state directly, you specify the mutations you want to happen with plain objects called actions. Then you write a special function called a reducer to decide how every action transforms the entire application's state.

If you're coming from Flux, there is a single important difference you need to understand. Redux doesn't have a Dispatcher or support many stores. Instead, there is just a single store with a single root reducing function. As your app grows, instead of adding stores, you split the root reducer into smaller reducers independently operating on the different parts of the state tree. This is exactly like there is just one root component in a React app, but it is composed out of many small components.

This architecture might seem like an overkill for a counter app, but the beauty of this pattern is how well it scales to large and complex apps. It also enables very powerful developer tools, because it is possible to trace every mutation to the action that caused it. You can record user sessions and reproduce them just by replaying every action.

Documentation

For PDF, ePub, and MOBI exports for offline reading, and instructions on how to create them, please see: paulkogel/redux-offline-docs.

Examples

If you're new to the NPM ecosystem and have troubles getting a project up and running, or aren't sure where to paste the gist above, check out simplest-redux-example that uses Redux together with React and Browserify.

Discussion

Join the #redux channel of the Reactiflux Discord community.

Thanks

Special thanks to Jamie Paton for handing over the redux NPM package name.

Logo

You can find the official logo on GitHub.

Change Log

This project adheres to Semantic Versioning.
Every release, along with the migration instructions, is documented on the Github Releases page.

Patrons

The work on Redux was funded by the community.
Meet some of the outstanding companies that made it possible:

See the full list of Redux patrons.

License

MIT

About

Redux Getting Started

http://redux.js.org/

License:MIT License


Languages

Language:JavaScript 96.6%Language:HTML 2.0%Language:SCSS 1.3%