Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
59 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,18 @@ | ||
import React, { Component } from 'react'; | ||
import logo from './logo.svg'; | ||
import './App.css'; | ||
import React, { useReducer } from 'react' | ||
import Counter from './Counter' | ||
import CounterContext from './contexts/CounterContext' | ||
import CounterReducer from './reducers/CounterReducer' | ||
import './App.css' | ||
|
||
class App extends Component { | ||
render() { | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<p> | ||
Edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<a | ||
className="App-link" | ||
href="https://reactjs.org" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn React | ||
</a> | ||
</header> | ||
</div> | ||
); | ||
} | ||
export default function App() { | ||
const [state, dispatch] = useReducer(CounterReducer, { count: 0 }) | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<CounterContext.Provider value={{ ...state, dispatch }}> | ||
<Counter /> | ||
</CounterContext.Provider> | ||
</header> | ||
</div> | ||
) | ||
} | ||
|
||
export default App; |
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,14 @@ | ||
import React, { useContext } from 'react' | ||
import CounterContext from './contexts/CounterContext' | ||
import { increment, decrement } from './actions/CounterActions' | ||
|
||
export default function Counter() { | ||
const { count, dispatch } = useContext(CounterContext) | ||
return ( | ||
<> | ||
<h2>{count}</h2> | ||
<button onClick={() => increment(dispatch)}>Increment</button> | ||
<button onClick={() => decrement(dispatch)}>Decrement</button> | ||
</> | ||
) | ||
} |
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,13 @@ | ||
export function increment(dispatch) { | ||
dispatch({ | ||
type: 'increment', | ||
payload: {} | ||
}) | ||
} | ||
|
||
export function decrement(dispatch) { | ||
dispatch({ | ||
type: 'decrement', | ||
payload: {} | ||
}) | ||
} |
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,6 @@ | ||
import { createContext } from 'react' | ||
|
||
export default createContext({ | ||
count: 0, | ||
dispatch: null | ||
}) |
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,10 @@ | ||
export default function CounterReducer(state, action) { | ||
switch (action.type) { | ||
case 'increment': | ||
return { count: state.count + 1 } | ||
case 'decrement': | ||
return { count: state.count - 1 } | ||
default: | ||
return state | ||
} | ||
} |