Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react如何实现组件预加载? #42

Open
artdong opened this issue Sep 11, 2019 · 1 comment
Open

react如何实现组件预加载? #42

artdong opened this issue Sep 11, 2019 · 1 comment
Labels
react reactjs

Comments

@artdong
Copy link
Collaborator

artdong commented Sep 11, 2019

react如何实现组件预加载?

@artdong artdong added the react reactjs label Sep 11, 2019
@artdong
Copy link
Collaborator Author

artdong commented Sep 12, 2019

动态import()

import React {Suspense,lazy} from 'react'

const LoadableComponent = lazy(() => import("./my-component"));

class Test extends React.Component {
  render () {
    return (
        <Suspense fallback={<div>Loading...</div>}>
            <LoadableComponent  />
        </Suspense>)
  }
}

export default Test

react-loadable

import React from 'react'
import loadable from 'react-loadable'

const LoadableComponent = Loadable({
  loader: () => import('./my-component'),
  loading: <div>Loading...</div>,
});

class Test extends React.Component {
  render () {
    return (
       <div>
           <LoadableComponent/>
       </div>)
  }
}

export default Test

tips

要安装配置babel-plugin-syntax-dynamic-import,否则会报语法错误

npm i react-loadable babel-plugin-syntax-dynamic-import -D

在.babelrc文件中添加配置

{
  "presets": [
    "react"
  ],
  "plugins": [
    "syntax-dynamic-import"
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react reactjs
Projects
None yet
Development

No branches or pull requests

1 participant