React

React Logo

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).

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