前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React Router Load On Demand

React Router Load On Demand

作者头像
IMWeb前端团队
发布2018-01-08 17:55:27
7030
发布2018-01-08 17:55:27
举报
文章被收录于专栏:IMWeb前端团队IMWeb前端团队

本文作者: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.

代码语言:javascript
复制
git clone https://github.com/chyingp/react-router-load-on-demand.git

Install react-tools to compile jsx to js

代码语言:javascript
复制
npm install -g react-tools

Run the example

Enter the example folder via command line

代码语言:javascript
复制
cd example

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

代码语言:javascript
复制
jsx -w jsx js

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

代码语言:javascript
复制
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.

Only home.js was loaded at first view.

Click the about link

The about component was shown.

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Some preparation
  • Run the example
  • See the result: component was loaded on demand
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档