Last active
June 4, 2020 05:34
-
-
Save chobi1125/537a0c5a22d011a2c22f4e784f7c355c to your computer and use it in GitHub Desktop.
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
import React, { useReducer } from "react"; | |
import ReactDOM from "react-dom"; | |
const initData = { | |
num: 0, | |
message: "クリックしてください" | |
} | |
const reducer = ({num} , action) => { | |
// eslint-disable-next-line | |
switch (action) { | |
case 'increment': | |
return {num:num + 1, message:`${num}に+1しました`} | |
case 'decrement': | |
return {num:num - 1, message:`${num}に-1しました`} | |
default: | |
return initData | |
} | |
}; | |
export const Counter = () => { | |
const [state, dispatch] = useReducer(reducer, initData); | |
return ( | |
<div> | |
<p>カウンターアプリ</p> | |
<p>{state.message}</p> | |
<p>クリック:{state.num}</p> | |
<p> | |
<button onClick={() => dispatch('decrement')}>-</button> | |
<button onClick={() => dispatch('increment')}>+</button> | |
<button onClick={() => dispatch()}>reset</button> | |
</p> | |
</div> | |
); | |
}; | |
ReactDOM.render(<Counter />, document.querySelector("#root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment