Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/martynaskadisa/react-lazy-import


https://github.com/martynaskadisa/react-lazy-import

code-splitting lazy-loading react typescript webpack

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# react-lazy-import

## DEPRECATED: Please use `React.lazy` or `@loadable/component` which is a recommended solution by React team

A simple higher order component for easy code splitting.

## Features

* Supports `import()`
* Supports react-router v4
* Ability to add ``
* Ability to Add ``

## Quick example
```jsx
import createLazyContainer from 'react-lazy-import';

const MyComponent = createLazyContainer(() => import('./MyComponent'));

const App = () => {
return (


My lazy component:


);
}
```

## Complete example

```jsx
// Greeter.jsx
import React from 'react';

const Greeter = ({ name }) => (

Hello {name}, I am lazy loaded

);

export default Greeter;
```

```jsx
// App.jsx
import React from 'react';
import { render } from 'react-dom';
import createLazyContainer from 'react-lazy-import';

const Loading = () =>

Loading...
;
const Error = () =>
Error!
;

const Greeter = createLazyContainer(() => import('./Greeter'), Loading, Error);

const App = () => {
return (


My lazy component:


);
}

render(, document.getElementById('app'));
```

### Usage with react-router

```jsx

import { Route, Switch } from 'react-router-dom';

const Loading: React.SFC<{}> = () =>

Loading...
;
const Error: React.SFC<{}> = () =>
Error!
;

const Home = createLazyContainer(() => import('./Home'), Loading, Error);
const About = createLazyContainer(() => import('./About'), Loading, Error);
const Contact = createLazyContainer(() => import('./Contact'), Loading, Error);
const NotFound = createLazyContainer(() => import('./NotFound'), Loading, Error);

class App extends React.Component {
render () {
return (






);
}
}
```

## API

### `createLazyContainer(loader, [loadingComponent], [errorComponent])`

* `loader: () => Promise`

Function returning promise which returns a React component.
* `loadingComponent` (optional)

React component which is rendered while your component is loading.
* `errorComponent` (optional)

React component which is rendered when loader fails to load your component