GithubHelp home page GithubHelp logo

cjamesb / isomorphicreactreduxtodo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gethyl/isomorphicreactreduxtodo

0.0 0.0 0.0 11.28 MB

A basic to-do list app which shows how to create an Isomorphic react-redux app.

CSS 0.97% HTML 10.50% JavaScript 88.53%

isomorphicreactreduxtodo's Introduction

Simple Isomorphic React-Redux To-Do List


Please use this example to learn/understand and not as a boilerplate.


A simple React-Redux Isomorphic To-Do List which will help you understand how to render a React-Redux app on server side.

  • You can add a new item.
  • Edit an item to change the task name.
  • Delete an item.
  • And finally, mark an item as complete when you are done with the task.

What is an isomorphic app?

As per the definition mentioned here.

JavaScript applications which run both client-side and server-side. Isomorphic JavaScript frameworks are the next step in the evolution of JavaScript frameworks. These new libraries and frameworks are solving the problems associated with traditional JavaScript frameworks.

The two distinct advantages going the isomorphic way is

  1. SEO friendly.
  2. Also with the current trend our apps do lot stuff in the client side. Due to this, usually you will notice during the initial render there is a delay. To avoid this we feed the initial load from the server thereby making it really fast.

Understanding the server side rendering works to build the isomorphic app

Jump into the code get your hands dirty and refer this section to understand how things from the server end is setup.

  1. We create a simple express server in server.js which will be the entry point for the server.
  2. When the page loads, we check if the url matches with the path from the react router and then
  • We create the redux store with allReducers and the initial state, initialState
 store = createStore(allReducers,initialState)

The structure of state which will be used for this Todo app is:-

  • items - The list of all the todo items.
  • id - Id number.
  • text - Text of each todo item.
  • editItem - This flag is used to open the model window to edit the todo item.
  • completed - This flag is used to mark an item as completed. Clicking on the item again will mark it as incomplete.

items:[ {id:number, text:string, editItem:boolean, completed:boolean } ]

  1. The server cannot render React items directly therefore reactDOMServer provides a function called renderToString which will render the react element and return and HTML string.
markup = renderToString(
                <Provider store={store}>
                   { <RouterContext {...renderProps} />}
                </Provider>

We provide the redux store as Provider just as we would do in client. But instead of using Router we using RouterContext for server and pass the props that we receive from match

  1. Finally we send the index.ejs file along with react rendered string and the initial state as response to the initial request made on the page.
return res.render('index', { markup: markup, initialState: initialState });
  1. We need to store the inital state in global/window variable so that we pass it to the client so that it can create the store with the same initial state. This is done by adding the below code in the index.ejs file within script tag.
window.__REDUX_STATE__ = JSON.parse('<%- JSON.stringify(initialState) %>');
  1. On receiving the response from the server, react checks if the server rendered HTML file is different from client. If it notices it is different, then client gets rendered and due to this you lose the advantage of server rendering as the client renders again.

  2. If your server rendering went fine with the initial state, then you will see the Todo list home page with a dummy task called Initial State To Do Item

  3. Client entry point is client.js

  4. Apart from the landing page, which is the Home page, there is only a dummy Help page. This is just to show you guys how to use bootstrap with react for navigation.


Getting Started

There are few npm packages which can be omitted. I will clean them up in the coming days.

  1. Install all the relevant packages (refer to package.json)

npm i -S package-name

  1. If you notice the script in package.json, there is dev & server
  • dev - is using webpack dev server to build the client related code and to bundle it at dist/js/bundle.js

npm run dev

  • server - This actually does webpack to build the client/ bundle them + uses babel-node to build/transpile ES6 code in server to ES5.

npm run server

On a side note using babel-node on production is bad idea, please refer the official babel documents on how to use babel instead.

  1. Follow a top down approach. First set up just the client and we can dive into the server later.

  2. Get a basic React-Redux client up and running with all the necessary actions/reducers and the store.

  3. Once you are clear with the how the app works in client side, dive into the server part. You can refer to server.js which is the entry point.

isomorphicreactreduxtodo's People

Contributors

gethyl 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.