Professional Documents
Culture Documents
Building a single-page CRUD app using React and Redux can be challenging
because you’ll have to deal w/ new techniques and terms like “Reducers”,
“Actions”, “Middlewares”, “Stores” and so on.
Perhaps the trickiest part is making async requests and handling responses.
While there are many examples, there is no well established pattern for
making async requests and handling responses in Redux apps(just yet).
In this blog I’ll provide a general approach on how to build a Blog app
that has 3 pages and show navigate b/w them.
Further, I’ll also establish a pattern for making async requests and
handling four async states: “loading”, “success”, “error” and “success-
and-navigate”.
Each page has “Success”, “Loading” and “Error” phases because they all
make AJAX calls to load/delete posts, so we need to mock those things as
well.
This helps you identify reusable components across different pages and also
any additional ones in a specific phase. For example, you may need a
“Spinner” or an “Error” component for different phases.
Note: this doesn’t need to be perfect. You can make changes later on.
3. New Post page: 1. Allows creating posts and 2. allows navigating back to
Index. Again we end up with PostForm and Header component.
Notice that we can reuse Header across all 3 pages because of the physical
location. So we end up with four components (instead of 6): 1. PostsList, 2.
PostDetails 3. PostForm and 4. Header components.
Redux Terms:
Redux Terms — “Actions” And “States”
Every component does two things:
1. Listen to the user and server events and send them to JS functions. In
Redux, events are represented as a JSON object called “Actions”.
2. Render DOM based on some data. This data is called a“state”, which is
also a JSON object.
function fetchPost(id) {
return {
type: FETCH_POST,
result: makeServerRequest("http://postsServer.com/api/id")
};
}
//Call the "Action Creator" w/ post's id and then use it's return
//value (Action JSON object) to finally dispatch it to "reducers"
The components calls Action creators to receive Actions and then dispatches the
actions. Redux then send the actions to “Reducers”
was sent to them via “dispatch”, apply action to the current state and
return a new state. And Redux re-renders all components whenever there
is a new state.
Actions:
1. FETCH_OBJ — Dispatch this to make the server request and also let
other components know we are loading. This helps current/other
components show “loading” or hide or do something.
Once Redux gets this and passes it through reducers, the new state will look
something like:
Once Redux gets this and passes it through reducers, the new state will look
something like:
Once Redux gets this and passes it through reducers, the new state will look
something like:
Once Redux gets this and passes it through reducers, the new state will look
something like:
States:
List out various data that may change the display of the component in all
phases of the component.
1. Shows list of Posts. Let’s call the state as “posts” (an array).
2. Shows “Loading..”, if it’s in the processing fetching the posts. Let’s call
this state “loading”(boolean)
Since all the above are related to PostList, let’s put them in a single state
object called postList.
Actions:
This component makes a “AJAX” call to load posts, so we’ll use the above
mentioned pattern and create 4 actions.
1.Asks server for list of posts. Let’s call this action as: “FETCH_POSTS”.
return {
type: FETCH_POSTS,
payload: request
};
}
2.Tells every component that it received posts (success case). Let’s call this
“FETCH_POSTS_SUCCESS”
3.Tells every component that there was an error(failure case). Let’s call this
“FETCH_POSTS_FAILURE”
4. Resetting data is not required because this is the 1st page (you’ll see how
this is useful in other 2 pages)
return {
type: FETCH_POSTS,
payload: request
};
}
case FETCH_POSTS_SUCCESS:
return { …state, postsList: {posts: action.payload, error:null,
loading: false}
};
Note: In the actual code, I am using the awesome redux-form library for form-
validation. I’ll blog about it in a different post.
ReactDOM.render(
<Provider store={store}> <- Inject global redux state to comps
<Router history={browserHistory}>
<Route path=”/” component={App}> <- Wrapper for all pages
<IndexRoute component={PostsIndex} /> <-wrapper Index page
<Route path=”posts/new” component={PostsNew} /> <- New page
<Route path=”posts/:id” component={PostsShow} /> <-Details
</Route>
</Router>
</Provider>
, document.getElementById('body'));
My Other Blogs
LATEST:
1. [Video course] The Inner workings of the Browser — for JavaScript & Web
Developers Use code: INNER15 and get 50% off!
Functional Programming
1. JavaScript Is Turing Complete — Explained
ES6
1. 5 JavaScript “Bad” Parts That Are Fixed In ES6
WebPack
1. Webpack — The Confusing Parts
Draft.js
1. Why Draft.js And Why You Should Contribute
Salesforce
1. Developing React Redux Apps In Salesforce’s Visualforce
5.5K claps
WRITTEN BY
rajaraodv Follow