forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor with-context-api example to use functional components (verce…
- Loading branch information
Showing
6 changed files
with
96 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useReducer, useContext } from 'react' | ||
|
||
const CounterStateContext = React.createContext() | ||
const CounterDispatchContext = React.createContext() | ||
|
||
const reducer = (state, action) => { | ||
switch (action.type) { | ||
case 'INCREASE': | ||
return state + 1 | ||
case 'DECREASE': | ||
return state - 1 | ||
case 'INCREASE_BY': | ||
return state + action.payload | ||
default: | ||
throw new Error(`Unkown action: ${action.type}`) | ||
} | ||
} | ||
|
||
export const CounterProvider = ({ children }) => { | ||
const [state, dispatch] = useReducer(reducer, 0) | ||
return ( | ||
<CounterDispatchContext.Provider value={dispatch}> | ||
<CounterStateContext.Provider value={state}> | ||
{children} | ||
</CounterStateContext.Provider> | ||
</CounterDispatchContext.Provider> | ||
) | ||
} | ||
|
||
export const useCount = () => useContext(CounterStateContext) | ||
export const useDispatchCount = () => useContext(CounterDispatchContext) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
import React, { Component } from 'react' | ||
/* First we import the consumer */ | ||
import { CounterConsumer } from '../components/CounterProvider' | ||
import React from 'react' | ||
import Link from 'next/link' | ||
import { useCount, useDispatchCount } from '../components/Counter' | ||
|
||
export default class about extends Component { | ||
render () { | ||
return ( | ||
/* Then we use our context through render props */ | ||
<CounterConsumer> | ||
{({ count, increase, increaseBy }) => ( | ||
<div> | ||
<h1>ABOUT</h1> | ||
<p>Counter: {count}</p> | ||
<button onClick={increase}>Increase</button> | ||
<button | ||
onClick={() => { | ||
increaseBy(15) | ||
}} | ||
> | ||
Increase By 15 | ||
</button> | ||
<p> | ||
<Link href='/'> | ||
<a>Home</a> | ||
</Link> | ||
</p> | ||
</div> | ||
)} | ||
</CounterConsumer> | ||
) | ||
} | ||
const AboutPage = () => { | ||
const count = useCount() | ||
const dispatch = useDispatchCount() | ||
|
||
const handleIncrease = event => | ||
dispatch({ | ||
type: 'INCREASE' | ||
}) | ||
const handleIncrease15 = event => | ||
dispatch({ | ||
type: 'INCREASE_BY', | ||
payload: 15 | ||
}) | ||
|
||
return ( | ||
<> | ||
<h1>ABOUT</h1> | ||
<p>Counter: {count}</p> | ||
<button onClick={handleIncrease}>Increase</button> | ||
<button onClick={handleIncrease15}>Increase By 15</button> | ||
<p> | ||
<Link href='/'> | ||
<a>Home</a> | ||
</Link> | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
export default AboutPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
import React, { Component } from 'react' | ||
import React from 'react' | ||
import Link from 'next/link' | ||
/* First we import the consumer */ | ||
import { CounterConsumer } from '../components/CounterProvider' | ||
import { useCount, useDispatchCount } from '../components/Counter' | ||
|
||
export default class index extends Component { | ||
render () { | ||
return ( | ||
/* Then we use our context through render props */ | ||
<CounterConsumer> | ||
{({ count, increase, decrease }) => ( | ||
<div> | ||
<h1>HOME</h1> | ||
<p>Counter: {count}</p> | ||
<button onClick={increase}>Increase</button> | ||
<button onClick={decrease}>Decrease</button> | ||
<p> | ||
<Link href='/about'> | ||
<a>About</a> | ||
</Link> | ||
</p> | ||
</div> | ||
)} | ||
</CounterConsumer> | ||
) | ||
} | ||
const IndexPage = () => { | ||
const count = useCount() | ||
const dispatch = useDispatchCount() | ||
|
||
const handleIncrease = event => | ||
dispatch({ | ||
type: 'INCREASE' | ||
}) | ||
const handleDecrease = event => | ||
dispatch({ | ||
type: 'DECREASE' | ||
}) | ||
|
||
return ( | ||
<> | ||
<h1>HOME</h1> | ||
<p>Counter: {count}</p> | ||
<button onClick={handleIncrease}>Increase</button> | ||
<button onClick={handleDecrease}>Decrease</button> | ||
<p> | ||
<Link href='/about'> | ||
<a>About</a> | ||
</Link> | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
export default IndexPage |