Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

Commit d7f6425

Browse files
author
mattjstar
committed
more quick edits
1 parent f42258a commit d7f6425

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

_drafts/react-reflux-to-redux.markdown

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
---
22
layout: post
3-
title: React Tutorial - From Flux (Reflux) to Redux
3+
title: React Tutorial - Converting Reflux to Redux
44
author: Matt Star
55
summary:
66
image: http://res.cloudinary.com/wework/image/upload/s--xpIlilub--/c_scale,q_jpegmini:1,w_1000/v1443207604/engineering/shutterstock_294201896.jpg
77
categories: process
88
---
99

10-
There are many great tutorials and boilerplate apps online--linked to in the footer of this post--and even documentation for redux and react itself is fantastic, so for the purposes of this tutorial I'd like to focus on purely how we switched from using reflux to redux in production. You don't necesarilly need a background in Reflux or Redux to understand what's going on, but at least a working knowledge of React is recommended--bonus points for [flux](https://facebook.github.io/flux/docs/overview.html) knowledge as well. This tutorial was initilly written to onboard other WeWork engineers, hence its density, and is intended for beginners and experts alike.
10+
There are many great tutorials and boilerplate apps online--linked to in the footer of this post--and even documentation for redux and react itself is fantastic, so for the purposes of this tutorial I'd like to focus on purely how we switched from using reflux to redux in production. You don't necesarilly need a background in Reflux or Redux specifically to understand what's going on, but at least a working knowledge of React and [Flux](https://facebook.github.io/flux/docs/overview.html) is recommended.
1111

12-
Over the last week or so we took the time to convert our ES5 React Reflux implementation of our [location's flow](https://www.wework.com/locations/new-york-city) into a brand spanking new ES6/7 React ***Redux*** implemenation. We'll go over 3 important examples in our data flow: saving the state of the ui in a store, fetching data from an external API to hydrate our store, and filtering data that is already in our store. This might be redundant, but you'll also get some transitioning from ES5 to ES6/ES7 bonus tips.
12+
Over the last week or so we took the time to convert our ES5 React Reflux implementation of our [location's flow](https://www.wework.com/locations/new-york-city) into a brand spanking new ES6/7 React ***Redux*** implemenation. We'll go over 3 important examples in our data flow: saving the state of the ui in a store, fetching data from an external API to hydrate our store, and filtering data that is already in our store. This might be redundant, but you'll also get some transitioning from ES5 to ES6/ES7 examples built in.
1313

1414
## EXAMPLE 1: Store the State of your UI in your...Store
1515

16-
One of the best parts or React, or any componentized framework for that matter is that it's a *declarative* framework--"telling the 'machine' what you would like to happen, and let the computer figure out how to do it"--vs an *imperative* framework--"telling the machine how to do something, and as a result what you want to happen will happen" ([source](http://latentflip.com/imperative-vs-declarative/)).
17-
1816
Let's think about something as simple as hiding or showing extra filters in a list application.
1917

2018
Going from a state where most of the location filters are hidden, with a link that says "More Filters"...
@@ -27,7 +25,7 @@ Going from a state where most of the location filters are hidden, with a link th
2725

2826
### jquery - the old standby:
2927

30-
In an *imperative* example using jquery you could have a listener to that specific link, and when it's clicked have it toggle the hidden filters container:
28+
Using jQuery you could have a listener to that specific link, and when it's clicked have it toggle the hidden filters container:
3129

3230
```js
3331
$(".toggle-filters-link").click(function() {
@@ -37,24 +35,27 @@ $(".toggle-filters-link").click(function() {
3735

3836
Problem with this is, it's super specific, it's not the most reusable piece of code in the world, and it usually just gets dumped into a compiled application.js file that will be hard to find in a production level codebase.
3937

40-
How can we do better???
38+
### Reflux - the first attempt:
4139

42-
Let's now look at the more *declarative* example with our initial Reflux implementation.
40+
What if we had access to a boolean that told us whether we should show or hide the hidden filters container? Our React components would listen to that boolean so it would know what to display, then our link component would simply dispatch an action that would toggle that boolean when clicked.
4341

44-
### Reflux - the first attempt:
42+
Quick flux refresher from their docs:
4543

46-
What if we had access to a boolean that told us whether we should show or hide the hidden filters container? Our React components would listen to that boolean so it would know what to display (...*declarative*), then our link component would simply dispatch an action that would toggle that boolean when clicked.
44+
![Flux Chart](https://facebook.github.io/flux/img/flux-simple-f8-diagram-explained-1300w.png)
4745

4846
Following the [Reflux pattern](https://github.com/reflux/refluxjs), let's declare an action--showFiltersActions--to tell the store that we want to toggle the state of "showFilters", and let's create a single store--showFiltersStore--to hold the record of this state.
4947

5048
```js
49+
50+
////////// ACTION: //////////
5151
// src/actions/show_filters_actions.js
5252
var ShowFiltersActions = {};
5353
ShowFiltersActions.toggle = Reflux.createAction();
5454
module.exports = ShowFiltersActions;
5555

5656

5757

58+
////////// STORE: //////////
5859
// src/stores/show_filters_store.js
5960
var ShowFiltersActions = require('../actions/show_filters_actions');
6061

@@ -165,10 +166,10 @@ module.exports = LocationFilters;
165166
I know this might seem like overkill essentially replacing 3 lines of jquery and a few lines of html with 4 different files between the actions, stores, and components, but this method actually leads to cleaner more reusable--and in my opinion more readable--code, as you start to extend the functionality of your application. What if you wanted to carry the state of your UI through multiple pages on your application? How do you handle reloads? React allows you to handle this complexity in a structured *declarative* manner.
166167

167168

168-
But this tutorial isn't about why you should use React. What about Reflux???
169+
But this tutorial isn't about why you should use Reflux...
169170

170171

171-
### Redux - Finally:
172+
### Redux:
172173

173174
When you start building larger production level applications that require many types of stores and a more complex data model, reflux can start to feel a little bloated (TERRIBLE PUN). Redux is a natural successor to reflux (and other flux patterns in general).
174175

@@ -190,7 +191,7 @@ Let's look at the Redux implementation of our showFiltersLink.
190191
A few caveats before diving into the new code:
191192

192193
* We adopted the [ducks/modules](https://github.com/erikras/ducks-modular-redux) pattern from the [The React/Redux/HotReloader Boilerplate app](https://github.com/erikras/react-redux-universal-hot-example) when building our Redux actions and reducers. This is why our implementation might look slightly different from those in the Redux docs.
193-
* As we went through the process of converting Reflux to Redux we also updated our code to the new ES6 syntax.
194+
* As we went through the process of converting Reflux to Redux we also updated our code to ES6.
194195

195196
#### Redux: Building a module
196197

@@ -199,7 +200,7 @@ The single store concept can seem a little strange coming from the reflux world.
199200
```js
200201
{
201202
showFilters: true,
202-
locations [
203+
locations: [
203204
{ name: "Bryant Park" },
204205
{ name: "42nd St"},
205206
etc...
@@ -265,7 +266,7 @@ export default combineReducers({
265266

266267
// {
267268
// showFilters: true,
268-
// locations [
269+
// locations: [
269270
// { name: "Bryant Park" },
270271
// { name: "42nd St"},
271272
// etc...
@@ -513,7 +514,7 @@ export function fetchLocationsIfNeeded() {
513514

514515
A nice feature to this pattern is we dispatch the `receiveLocations()` action once data is received from the API. That forces a state change which causes our components to re-render, updating the view.
515516

516-
With the new locations module written, and everything already setup in EXAMPLE 1 to handle this new locations reducer. All we have to do is now connect to this part of the store in our main `<App />` component.
517+
With the new locations module written, and everything already setup in EXAMPLE 1 to handle this new locations reducer, all we have to do is now connect to this part of the store in our main `<App />` component.
517518

518519
```js
519520
// src/containers/app.js

0 commit comments

Comments
 (0)