栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

动态加载反应组分

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

动态加载反应组分

基本上,它可以归结为预先创建您将需要的所有块。然后,您只需要一种动态引用它们的方法。这是我基于的解决方案:

http://henleyedition.com/implicit-pre-splitting-with-react-router-and-
webpack

这是我不使用React Router的方法(注意:我发现它与redux或动画没有很好的匹配):

//loader:{  test: (folder)/.*.js,  include: path.resolve(__dirname, 'src')  loader: ['lazy?bundle', 'babel']}//dynamic usage within React component:const My_COMPonENTS = {   ComponentA: require('./folder/ComponentA'),   ComponentB: require('./folder/ComponentB'),}class ParentComponent extends React.Component {    componentDidMount() {        My_COMPONENTS[this.props.name](component => this.setState({component}));    }     render() {       return <this.state.component />;    }}

因此,结果是您正在动态渲染组件,但是是从一组静态的预先确定的可能性中进行的-同时,仅向客户端发送的内容不多于访问者实际感兴趣的部分。

另外,这是我做得很好的一个组件:

import React from 'react';import Modal from './widgets/Modal';export default class AjaxModal extends React.Component {  constructor(props, context) {    super(props, context);    this.state = {      Content: null    };  }  componentDidMount() {    if(this.props.show) {      this.loadContent();    }  }  componentWillReceiveProps({show}) {    if(show && !this.state.Content) {      this.loadContent(1200); //dont interfere with animation    }  }  loadContent(ms=0) {    setTimeout(() => {      this.props.requestLazyBundle(({default: Content}) => {        this.setState({Content});      });    }, ms);  }  render() {    let {Content} = this.state;    return (      <Modal title={this.props.title} {...this.props} loading={!Content}>        {Content ? <Content /> : null}      </Modal>    );  }}

通过传递async require bundler函数

this.props.requestLazybundle
,如下所示:

render() {  let requestLazyBundle = require('bundle?lazy&name=AnotherComponent!../content/AnotherComponent');  return (    <AjaxModal title='Component Name' {...props} requestLazyBundle={requestLazyBundle} />  );}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号