ReactJS - Code Splitting _route code splitting asyncComponent
asyncComponent 항수는 파라미터로 컴포넌트를 불러오는 함수를 받아와서 성태관리를 자동으로 해준다.
~cra-router/src/lib$ asyncRoute.js
import React from 'react';
export default function asyncComponent (getComponent) {
return class AsyncComponent extends React.Component {
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(({default: Component}) => {
this.setState({ Component })
})
}
}
render() {
const { Component } = this.state
if (Component) {
return <Component {...this.props} />
}
return null
}
}
}
~cra-router/src/routes$ index.async.js
import asyncRoute from '../lib/asyncRoute';
export const Home = asyncRoute(() => import('./Home'));
export const About = asyncRoute(() => import('./About'));
export const Login = asyncRoute(() => import('./Login'));
export const MyPage = asyncRoute(() => import('./MyPage'));
export const Posts = asyncRoute(() => import('./Posts'));
export const Search = asyncRoute(() => import('./Search'));
export const NotFound = asyncRoute(() => import('./NotFound'));
~cra-router/src$ App.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { Home, About, Posts, Login, MyPage, Search, NotFound } from './routes/index.async.js'
/....../
'my_lesson > _ReactJS' 카테고리의 다른 글
React-router - react-hot-loader (0) | 2018.04.14 |
---|---|
React-router - Code Splitting _webpack Prodoction (0) | 2018.04.13 |
React-router - CodeSplitting(prodoction) _ Create chunk (0) | 2018.04.12 |
React-router - Code Splitting(prodoction) _Environment (0) | 2018.04.12 |
React-router - NotFound (Switch) (0) | 2018.04.11 |
댓글