Last active
July 27, 2020 00:26
-
-
Save felipegenuino/18353a2d42825258ebaaf86db1aed35f to your computer and use it in GitHub Desktop.
React Hooks
This file contains hidden or 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, { useState } from "react"; | |
import "./styles.css"; | |
export default function App() { | |
const [counter, setCounter] = useState(0); | |
function handlePlus() { | |
return setCounter(counter + 1); | |
} | |
function handleMinus() { | |
return setCounter(counter - 1); | |
} | |
return ( | |
<> | |
<div className="App">{counter}</div> | |
<button onClick={handlePlus}> + </button> | |
<button onClick={handleMinus}> - </button> | |
</> | |
); | |
} |
This file contains hidden or 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, { useState, useEffect } from "react"; | |
import "./styles.css"; | |
export default function App() { | |
const [counter, setCounter] = useState(0); | |
const [name, setName] = useState(""); | |
useEffect(() => { | |
console.log("first render"); | |
}, []); | |
useEffect(() => { | |
console.log(counter); | |
}, [counter]); | |
useEffect(() => { | |
console.log(name); | |
}, [name]); | |
function handleAdd() { | |
setCounter(prevState => prevState + 1); | |
} | |
function handleMinus() { | |
setCounter(prevState => prevState - 1); | |
} | |
return ( | |
<div> | |
<div className="App">{counter}</div> | |
<button onClick={handleAdd}> + </button> | |
<button onClick={handleMinus}> - </button> | |
<br /> | |
<span>{name}</span> | |
<input onChange={e => setName(e.target.value)} /> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment