陈映平

2015-08-08 16:19

React Router Load On Demand

本文作者:IMWeb 陈映平 原文出处:IMWeb社区 未经同意,禁止转载

It is frequently asked that how to load components on demand if you use react with react-router.

In the official readme, there is a recommended loader named react-router-proxy-loader that can fullfill this need.

However, it depends on webpack and there are many cases where webpack is not included in the build task.

In fact, it is quite easy to achieve the same goal without webpack. Here is an example with little dependencies, and if you see example/jsx/loader.js, you will find that it is nearly the same as react-router-proxy-loader, except that you don't need to have webpack installed.

Some preparation

Checkout this repo by git clone or download the ziped file.

git clone https://github.com/chyingp/react-router-load-on-demand.git

Install react-tools to compile jsx to js

npm install -g react-tools

Run the example

Enter the example folder via command line

cd example

Then run jsx to compile jsx format file to normal js file.

jsx -w jsx js

Next start a mini server. (Thanks to https://gist.github.com/ryanflorence/701407)

node server.js

Finally, visits it in browser http://localhost:8888/

See the result: component was loaded on demand

You can see the beautiful page.

image

Only home.js was loaded at first view.

image

Click the about link

image

The about component was shown.

image

Only at this time, the about component was loaded on demand.

image

1条评论

    您需要 注册 一个IMWeb账号或者 才能进行评论。