What is useState? — React Technical Interview Questions & Answers

What is useState?

What is useState?

  • In one line: useState is how a function component remembers a value between renders. Without it, every render would reset let n = 0. With it, n survives and the UI can change.
  • It is a hook — a function whose name starts with use. React only lets you call hooks in components (or other hooks). That is why the question is worded ‘what is the useState in React’ — they want hook + memory + re-render, not a blog slogan.
  • What is useState? — on screen: Hello Asha. useState holds name inside the function component.
  useState(false)
      │ click
      ▼
  setLiked(true)
      │
      ▼
   Like → Liked

Technical Interview Tip: Memory between renders + setter.

Code Examples for What is useState?

Example

// What is useState
import { useState } from "react";

export default function NameBox() {
  const [name, setName] = useState("Asha");
  return <p>Hello {name}</p>;
}

What is useState? — on screen: Hello Asha. useState holds name inside the function component.

How to Explain What is useState? to an Interviewer

What problem does useState solve vs a plain let?

let resets every render