Skip to content
This repository was archived by the owner on Jul 28, 2020. It is now read-only.

Commit 610a2e5

Browse files
author
Sergio Xalambrí
committed
Use Suspense, StrictMode, lazy and memo
1 parent 6db9f72 commit 610a2e5

File tree

6 files changed

+79
-19
lines changed

6 files changed

+79
-19
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!public
3+
!src
4+
!package.json
5+
!yarn.lock

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM mhart/alpine-node:10
2+
ARG CI
3+
WORKDIR /usr/src
4+
COPY package.json yarn.lock /usr/src/
5+
RUN yarn install
6+
COPY . .
7+
RUN yarn test
8+
RUN yarn build
9+
RUN mv /usr/src/build /public

now.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "react-lazy-memo",
3+
"alias": "react-lazy-memo.now.sh",
4+
"type": "static",
5+
"build": {
6+
"env": {
7+
"CI": "true"
8+
}
9+
},
10+
"static": {
11+
"rewrites": [
12+
{
13+
"source": "**",
14+
"destination": "/index.html"
15+
}
16+
]
17+
}
18+
}

src/App.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
import React, { Component } from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React, { Component, Suspense, lazy, memo } from "react";
2+
import "./App.css";
3+
4+
const sleep = ms => new Promise(r => setTimeout(r, ms));
5+
6+
const Logo = lazy(async () => {
7+
await sleep(1000);
8+
return import("./logo.js");
9+
});
10+
11+
const Placeholder = memo(() => <strong>Loading...</strong>);
412

513
class App extends Component {
14+
state = {
15+
alt: 'React',
16+
}
17+
async componentDidMount() {
18+
await sleep(5000);
19+
this.setState({ alt: 'React Lazy' });
20+
}
621
render() {
722
return (
823
<div className="App">
924
<header className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<p>
12-
Edit <code>src/App.js</code> and save to reload.
13-
</p>
14-
<a
15-
className="App-link"
16-
href="https://reactjs.org"
17-
target="_blank"
18-
rel="noopener noreferrer"
19-
>
20-
Learn React
21-
</a>
25+
<Suspense maxDuration={300} fallback={<Placeholder />}>
26+
<Logo alt={this.state.alt} />
27+
</Suspense>
2228
</header>
2329
</div>
2430
);

src/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import React from 'react';
1+
import React, { StrictMode, unstable_ConcurrentMode as ConcurrentMode } from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
5+
// import * as serviceWorker from './serviceWorker';
66

7-
ReactDOM.render(<App />, document.getElementById('root'));
7+
ReactDOM.render(
8+
<ConcurrentMode>
9+
<StrictMode>
10+
<App />
11+
</StrictMode>
12+
</ConcurrentMode>,
13+
document.getElementById('root')
14+
);
815

916
// If you want your app to work offline and load faster, you can change
1017
// unregister() to register() below. Note this comes with some pitfalls.
1118
// Learn more about service workers: http://bit.ly/CRA-PWA
12-
serviceWorker.unregister();
19+
// serviceWorker.unregister();

src/logo.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React, { memo } from "react";
2+
import logo from './logo.svg';
3+
4+
function Logo({ alt }) {
5+
return (
6+
<figure>
7+
<img src={logo} className="App-logo" alt={alt} />
8+
<figcaption>{alt}</figcaption>
9+
</figure>
10+
);
11+
}
12+
13+
export default memo(Logo, (prevProps, nextProps) => {
14+
return prevProps.alt === nextProps.alt;
15+
});

0 commit comments

Comments
 (0)