Created
January 26, 2020 20:52
-
-
Save polyrand/417c239320f022e974dadbba6139036b to your computer and use it in GitHub Desktop.
useState snippet
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, { useState } from "react"; | |
import ReactDOM from "react-dom"; | |
// import "./App.css"; | |
class ClassExample extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { count: 0 }; | |
} | |
render() { | |
return ( | |
<div> | |
<p>You clicked {this.state.count} times</p> | |
<button onClick={() => this.setState({ count: this.state.count + 1 })}> | |
Click me! | |
</button> | |
</div> | |
); | |
} | |
} | |
function Example(props) { | |
const [count, setCount] = useState(0); | |
const [arrst, setArr] = useState({ count: 4 }); | |
return ( | |
<div> | |
<p>You clicked {count} times</p> | |
<button onClick={() => setCount(count + 1)}>Click me!</button> | |
<p>You clicked (arr) {arrst.count} times</p> | |
<button | |
onClick={() => { | |
const newObj = { count: arrst.count + 1 }; | |
setArr(newObj); | |
}} | |
> | |
Click me dict! | |
</button> | |
</div> | |
); | |
} | |
function App() { | |
return ( | |
<div> | |
<h1>test!</h1> | |
<ClassExample /> | |
<Example /> | |
</div> | |
); | |
} | |
ReactDOM.render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment