react怎么实现按需加载 - 网站

react怎么实现按需加载

分类:React入门教程_React框架基础教程 · 发布时间:2020-09-23 09:38 · 阅读:1534

react-router4 实现按需加载

按需加载的背景

React Router 是一个非常出色的路由解决方案,同时也非常容易上手。但是当网站规模越来越大的时候,首先出现的问题是 Javascript 文件变得巨大,这导致首页渲染的时间让人难以忍受。实际上程序应当只加载当前渲染页所需的 JavaScript,也就是大家说的“代码分拆" — 将所有的代码分拆成多个小包,在用户浏览过程中按需加载。

老的方案

这里所说的老是指使用react-router低于4的版本。

在低版本(小于4)的react-router中直接提供了按需加载的方案,示例如下:

const rootRoute = {
  path: '/',
  indexRoute: {
    getComponent(nextState, cb) {
      require.ensure([], (require) => {
        cb(null, require('components/layer/HomePage'))
      }, 'HomePage')
    },
  },
  getComponent(nextState, cb) {
    require.ensure([], (require) => {
      cb(null, require('components/Main'))
    }, 'Main')
  },
  childRoutes: [
    require('./routes/baidu'),
    require('./routes/404'),
    require('./routes/redirect')
  ]
}
ReactDOM.render(
  (
    <Router
      history={browserHistory}
      routes={rootRoute}
      />
  ), document.getElementById('app')
);

核心代码是router.getComponent,然而在react-router4中,没有router.getComponent了,这样我们该如何实现按需加载呢?

react-router4的实现方案

根据官网的介绍:

One great feature of the web is that we don’t have to make our visitors download the entire app before they can use it.
You can think of code splitting as incrementally downloading the app. While there are other tools for the job, we’ll use Webpack and the bundle loader in this guide.

我们要借助bundle-loader来实现按需加载。

首先,新建一个bundle.js文件:

import React, { Component } from 'react'
export default class Bundle extends React.Component {
    state = {
        // short for "module" but that's a keyword in js, so "mod"
        mod: null
    }
    componentWillMount() {
        this.load(this.props)
    }
    componentWillReceiveProps(nextProps) {
        if (nextProps.load !== this.props.load) {
            this.load(nextProps)
        }
    }
    load(props) {
        this.setState({
            mod: null
        })
        props.load((mod) => {
            this.setState({
                // handle both es imports and cjs
                mod: mod.default ? mod.default : mod
            })
        })
    }
    render() {
        if (!this.state.mod)
            return false
        return this.props.children(this.state.mod)
    }
}

然后,在入口处使用按需加载:

// ...
// bundle模型用来异步加载组件
import Bundle from './bundle.js';
// 引入单个页面(包括嵌套的子页面)
// 同步引入
import Index from './app/index.js';
// 异步引入
import ListContainer from 'bundle-loader?lazy&name=app-[name]!./app/list.js';
const List = () => (
    <Bundle load={ListContainer}>
        {(List) => <List />}
    </Bundle>
)
// ...
    <HashRouter>
        <Router basename="/">
            <div>
                <Route exact path="/" component={Index} />
                <Route path="/list" component={List} />
            </div>
        </Router>
    </HashRouter>
// ...

webpack.config.js文件配置

output: {
    path: path.resolve(__dirname, './output'),
    filename: '[name].[chunkhash:8].bundle.js',
    chunkFilename: '[name]-[id].[chunkhash:8].bundle.js',
},

完整代码示例

bundle.js(https://github.com/Molin123/react-molin/blob/master/src/bundle.js)

react-molin(https://github.com/Molin123/react-molin)

更多相关技术文章,请访问HTML中文网

标签:
react

相关文章

react中怎么使用sass?

方法:1、使用npm工具安装sass-loader和node-sass依赖;2、配置webpack.config.js文件;3、在需要的组件中引入scss,然后添加scss代码即可。

react怎么判断checkbox是否被选中

react判断checkbox是否被选中的方法:可以通过【if ($(&quot;#checkbox-id&quot;).get(0).checked) {}】方法来进行判断。还可以通过原生js方法来进行判断,如【ele.checked】。

react子组件如何向父组件传值?

在react中,首先通过父组件给子组件传递一个函数,然后子组件通过参数传到父组件,通过props来传递函数的引用,并通过回调的方式实现;其实就是子组件调用父组件的方法,把值以形参的方式传出来。

react组件什么时候会重新渲染?

每当组件状态改变时,React就会调用render函数进行重新渲染。更改状态意味着当调用setState函数时,React触发更新;这不仅意味着将调用组件的render函数,而且还意味着重新渲染其所有后续子组件,无论其prop是否已更改。

react组件生命周期有几个阶段?

React组件生命周期有三个阶段:加载、更新和卸载。加载阶段表示一个组件实例被创建并被插入到DOM中;更新阶段表示由状态或属性的改变导致组件的重新渲染;卸载阶段表示组件从DOM中移除。

返回分类 返回首页