Last active
December 29, 2018 11:41
-
-
Save inPhoenix/6ebbfcc7fd471a4afbd09e9bee1f5589 to your computer and use it in GitHub Desktop.
React Hook Simple Example
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
// https://daveceddia.com/intro-to-hooks/ | |
import React, { useState } from 'react'; | |
import { render } from 'react-dom'; | |
function OneTimeButton(props) { | |
const [clicked, setClicked] = useState(true) // setClicked update the clicked state. | |
function clickedTest() { | |
props.onClick() | |
setClicked(!clicked) | |
console.log('%c clicked', 'color: #243b79 ', clicked); | |
} | |
return ( | |
<button onClick={clickedTest}> | |
You Can Only Click Me Once | |
</button> | |
); | |
} | |
function sayHi() { | |
console.log('hi'); | |
} | |
render( | |
<OneTimeButton onClick={sayHi}/>, | |
document.querySelector('#root') | |
); | |
// Install using: yarn add react@next react-dom@next | |
// Package.json Dependencies: | |
// "dependencies": { | |
// "react": "^16.7.0-alpha.0", | |
// "react-dom": "^16.7.0-alpha.0", | |
// "react-scripts": "2.0.4" | |
// }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment