React Basics - useState Hook

React Basics: useState Hook

The useState hook is used in functional components in order to use state (whereas state was once only available to class components).

The very basic code to implement the useState hook. Taken from the scrimba.com course https://scrimba.com/p/pMvMEuD/cWQw9JH2

App.js

import React, {useState} from "react"

function App() {
    const [count, setCount] = useState(0) 
    const [answer, setAnswer] = useState("Yes")
	// whatever var you set a state hook for the convention is to set it's corresponding setVar 
    
    function increment() {
        setCount(prevCount => prevCount + 1)
    }
    
    function decrement() {
        setCount(prevCount => prevCount - 1)
    }
    
    return (
        <div>
            <h1>{count}</h1>
            <button onClick={increment}>Increment</button>
            <button onClick={decrement}>Decrement</button>
        </div>
    )
}

export default App



Link your website to this page! Copy and paste the URL below:
http://www.cfsnap.com/react/react-basics-usestate-hook/