31
React+Redux Workshops Marcin Grzywaczewski (@Killavus) Arkency

React.js+Redux Workshops

Embed Size (px)

Citation preview

Page 1: React.js+Redux Workshops

React+Redux Workshops

Marcin Grzywaczewski (@Killavus) Arkency

Page 2: React.js+Redux Workshops

What

• An app for managing conferences

• Planning conferences

• Scheduling conference days

• Accepting events from CFP (call for papers)

• Scheduling events within conference days

Page 3: React.js+Redux Workshops

How• React.js and Redux will be used to create an

interface

• HTML Mockups are prepared so you can base on it (I’ll be happy if you experiment with your own solutions, though)

• Project with an API and all needed libraries is provided

• One view = one React.js+Redux application

Page 4: React.js+Redux Workshops

React.js• Responsible for rendering HTML.

• A function which takes properties and returns HTML.

• You create components which are small pieces of your interface and you compose them together.

• React.js uses JSX syntax, which is very similar to HTML.

• There is a concept of state, but thanks to Redux it is unnecessary.

Page 5: React.js+Redux Workshops

Component as function

JSBin Code

Page 6: React.js+Redux Workshops

Component as React class

JSBin Code

Page 7: React.js+Redux Workshops

Composing components

Page 8: React.js+Redux Workshops

How to make it dynamic?

• You can’t change props!

• You can call ReactDOM.render again. It is fast, thanks to React.js internal algorithms.

• There is a concept of state, but we won’t use it beacuse you don’t need it.

Page 9: React.js+Redux Workshops

Dynamic component?

JSBin Code

Page 10: React.js+Redux Workshops

What if…

• You can have some piece of data needed to render a component.

• Every time a piece of data changes you re-render your component. Your component is notified somehow about the change.

• This is what Redux will provide to us.

Page 11: React.js+Redux Workshops

Redux• Manages the data flow in your application.

• You emit actions which are like parameters for Rails update endpoint (but named!). Actions are created by action creators.

• Actions modify state.

• State is stored within a store.

• How an action modifies state is determined by a reducer.

Page 12: React.js+Redux Workshops

Greeter with React+Redux

• A function is called an app when contains:

• React components

• Redux building blocks

• Let’s create a GreeterApp, step by step.

Page 13: React.js+Redux Workshops

Empty App

Page 14: React.js+Redux Workshops

Initial State

Page 15: React.js+Redux Workshops

Action Creator

A return value of this function is called an action.

Page 16: React.js+Redux Workshops

Reducer

Page 17: React.js+Redux Workshops

Reducer recap

• A reducer is a function which takes the current state and an action and returns the new state based on an action processed.

• The return value must be a new object. No mutated values allowed!

Page 18: React.js+Redux Workshops

Wrongly written reducer (mutation of state)

Page 19: React.js+Redux Workshops

Store

You may consider store as a Redux internal thing. All you need to do is to pass a reducer and initial state and

you are done.

Page 20: React.js+Redux Workshops

Components

Page 21: React.js+Redux Workshops

Components recap

• The new property will be passed - nameChanged which is a function passed to onChange of GreeterEdit.

• Components are blissfully unaware of being managed by Redux ;).

• But how to connect React + Redux together?

Page 22: React.js+Redux Workshops

Connecting React & Redux• To connect React.js components with Redux stores

you need create a connector.

• Connector is a pair of state mapper and dispatch mapper.

• State mapper takes your state and produces an object which will be merged to component’s properties.

• Dispatch mapper exposes dispatch function which passes actions to a store. It returns an object which will get merged to component’s properties too.

Page 23: React.js+Redux Workshops

Dispatch & State mappers

Notice how action creator is used to create an action emitted to a store. It is not a coincidence!

Page 24: React.js+Redux Workshops

connect function

• connect function takes your state mapper and dispatch mapper and creates a connector. It is a function.

• To make your component connected you just pass a component as an argument to a connector.

Page 25: React.js+Redux Workshops

Connector & Connected Component

Page 26: React.js+Redux Workshops

Provider

• We know how to map state to properties and we’re exposing functions for dispatching actions. But we don’t know where to dispatch our actions!

• There is a Provider component. You wrap your connected component with it. It takes store as a property.

Page 27: React.js+Redux Workshops

Provider usage & return value of the GreeterApp

Page 28: React.js+Redux Workshops

GreeterApp Usage

Page 29: React.js+Redux Workshops

GreeterApp Code

JSBin Code

Page 30: React.js+Redux Workshops

Summary• React.js is great for defining your user interface in

the declarative way.

• Redux is great for managing the data flow in a simple way.

• You can connect the two together, removing the problem of updating React components.

• Component’s built-in state is not needed. It is the most troubling part of React components!

Page 31: React.js+Redux Workshops

Good luck!