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

Add Fast Refresh Demo #16576

Merged
merged 8 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/basic-features/fast-refresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ description:

# Fast Refresh

<details open>
<summary><b>Examples</b></summary>
<ul>
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/fast-refresh-demo">Fast Refresh Demo</a></li>
</ul>
</details>

Fast Refresh is a Next.js feature that gives you instantaneous feedback on
edits made to your React components. Fast Refresh is enabled by default in all
Next.js applications on **9.4 or newer**. With Next.js Fast Refresh enabled,
Expand Down
34 changes: 34 additions & 0 deletions examples/fast-refresh-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
17 changes: 17 additions & 0 deletions examples/fast-refresh-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Fast Refresh Demo

Next.js ships with [Fast Refresh](https://nextjs.org/docs/basic-features/fast-refresh) which gives you instantaneous feedback on edits made to your React components.

This example shows an auto incrementing value, a classic counter and an error counter. Most edits should be visible within a second, without losing component state.

## Usage

Run the following command to get started:

```bash
yarn
yarn dev
# or
npm install
npm run dev
```
15 changes: 15 additions & 0 deletions examples/fast-refresh-demo/components/ClickCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useCallback, useState } from 'react'
import styles from './ClickCount.module.css'

export default function ClickCount() {
const [count, setCount] = useState(0)
const increment = useCallback(() => {
setCount((v) => v + 1)
}, [setCount])

return (
<button className={styles.btn} type="button" onClick={increment}>
Clicks: {count}
</button>
)
}
32 changes: 32 additions & 0 deletions examples/fast-refresh-demo/components/ClickCount.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
button.btn {
margin: 0;
border: 1px solid #d1d1d1;
border-radius: 5px;
padding: 0.5em;
vertical-align: middle;
white-space: normal;
background: none;
line-height: 1;
font-size: 1rem;
font-family: inherit;
transition: all 0.2s ease;
}

button.btn {
padding: 0.65em 1em;
background: #0076ff;
color: #fff;
border: none;
cursor: pointer;
transition: all 0.2s ease;
}
button.btn:focus {
outline: 0;
border-color: #0076ff;
}
button.btn:hover {
background: rgba(0, 118, 255, 0.8);
}
button.btn:focus {
box-shadow: 0 0 0 2px rgba(0, 118, 255, 0.5);
}
41 changes: 41 additions & 0 deletions examples/fast-refresh-demo/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
body {
font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', 'Helvetica',
'Arial', sans-serif;
padding: 20px 20px 60px;
max-width: 680px;
margin: 0 auto;
font-size: 16px;
line-height: 1.65;
word-break: break-word;
font-kerning: auto;
font-variant: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizeLegibility;
hyphens: auto;
}

h2,
h3,
h4 {
margin-top: 1.5em;
}

code,
pre {
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace, serif;
font-size: 0.92em;
color: #d400ff;
}

code:before,
code:after {
content: '`';
}

hr {
border: none;
border-bottom: 1px solid #efefef;
margin: 5em auto;
}
15 changes: 15 additions & 0 deletions examples/fast-refresh-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "fast-refresh-demo",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"license": "MIT"
}
5 changes: 5 additions & 0 deletions examples/fast-refresh-demo/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import '../global.css'

export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
56 changes: 56 additions & 0 deletions examples/fast-refresh-demo/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useCallback, useEffect, useState } from 'react'
import ClickCount from '../components/ClickCount'
import styles from '../components/ClickCount.module.css'

function throwError() {
console.log(
// The function body() is not defined
document.body()
)
}

function Home() {
const [count, setCount] = useState(0)
const increment = useCallback(() => {
setCount((v) => v + 1)
}, [setCount])

useEffect(() => {
const r = setInterval(() => {
increment()
}, 1000)
return () => {
clearInterval(r)
}
}, [increment])

return (
<main>
<h1>Home</h1>
<div>
<p>Auto Incrementing Value</p>
<p>Current value: {count}</p>
</div>
<hr />
<div>
<p>Component with State</p>
<ClickCount />
</div>
<hr />
<div>
<button
className={styles.btn}
type="button"
onClick={(e) => {
setTimeout(() => document.parentNode(), 0)
throwError()
}}
>
Throw an Error
</button>
</div>
</main>
)
}

export default Home