Skip to content

Commit 1f64082

Browse files
maurodaprotistimneutkens
authored andcommittedSep 14, 2018
Add with-context-api example (#5154)
* Add with-context-api example * Change next dependency to canary and fix CounterProvider import
1 parent 695f372 commit 1f64082

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
 

‎examples/with-context-api/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-context-api)
2+
3+
# Hello World example
4+
5+
## How to use
6+
7+
### Using `create-next-app`
8+
9+
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
10+
11+
```bash
12+
npx create-next-app --example with-context-api with-context-api-app
13+
# or
14+
yarn create next-app --example with-context-api with-context-api-app
15+
```
16+
17+
### Download manually
18+
19+
Download the example:
20+
21+
```bash
22+
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-context-api
23+
cd with-context-api
24+
```
25+
26+
Install it and run:
27+
28+
```bash
29+
npm install
30+
npm run dev
31+
# or
32+
yarn
33+
yarn dev
34+
```
35+
36+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
37+
38+
```bash
39+
now
40+
```
41+
42+
## The idea behind the example
43+
44+
This example shows how to use react context api in our app. Based on WesBos example.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { Component } from 'react'
2+
3+
/* First we will make a new context */
4+
const CounterContext = React.createContext()
5+
6+
/* Then create a provider Component */
7+
class CounterProvider extends Component {
8+
state = {
9+
count: 0
10+
}
11+
12+
increase = () => {
13+
this.setState({
14+
count: this.state.count + 1
15+
})
16+
}
17+
18+
decrease = () => {
19+
this.setState({
20+
count: this.state.count - 1
21+
})
22+
}
23+
24+
render () {
25+
return (
26+
<CounterContext.Provider
27+
value={{
28+
count: this.state.count,
29+
increase: this.increase,
30+
decrease: this.decrease
31+
}}
32+
>
33+
{this.props.children}
34+
</CounterContext.Provider>
35+
)
36+
}
37+
}
38+
39+
/* then make a consumer which will surface it */
40+
const CounterConsumer = CounterContext.Consumer
41+
42+
export default CounterProvider
43+
export { CounterConsumer }
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "with-context-api",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"next": "^7.0.0-canary.16",
11+
"react": "^16.0.0",
12+
"react-dom": "^16.0.0"
13+
},
14+
"license": "ISC"
15+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import App, { Container } from 'next/app'
2+
/* First we import our provider */
3+
import CounterProvider from '../components/CounterProvider'
4+
5+
class MyApp extends App {
6+
render () {
7+
const { Component, pageProps } = this.props
8+
return (
9+
<Container>
10+
{/* Then we wrap our components with the provider */}
11+
<CounterProvider>
12+
<Component {...pageProps} />
13+
</CounterProvider>
14+
</Container>
15+
)
16+
}
17+
}
18+
19+
export default MyApp
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { Component } from 'react'
2+
/* First we import the consumer */
3+
import { CounterConsumer } from '../components/CounterProvider'
4+
5+
export default class index extends Component {
6+
render () {
7+
return (
8+
/* Then we use our context through render props */
9+
<CounterConsumer>
10+
{({ count, increase, decrease }) => (
11+
<div>
12+
<p>Counter: {count}</p>
13+
<button onClick={increase}>Increase</button>
14+
<button onClick={decrease}>Decrease</button>
15+
</div>
16+
)}
17+
</CounterConsumer>
18+
)
19+
}
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.