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 course


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 (
            <button onClick={increment}>Increment</button>
            <button onClick={decrement}>Decrement</button>

export default App

Link your website to this page! Copy and paste the URL below: