Importing useState from React
What you'll learn: How to properly import useState so you can add state to your function components.
Why You Need to Import useState
Before you can use useState in your component, you must import it from the React library. Unlike some things that are automatically available, useState is not global — React requires you to explicitly bring it into your file.
Think of it like borrowing a specific tool from a toolbox. React is the toolbox, and useState is one particular tool inside it. You need to tell JavaScript, "Hey, I want to use that specific tool."
The Correct Import Statement
At the very top of your component file, write:
import { useState } from 'react';
Notice the curly braces around useState. This is crucial.
Named Export vs Default Export
useState is a named export from React. This means:
- React exports many things (like
useState,useEffect,useRef, etc.) - You must use curly braces to pick the specific ones you want
- You must spell it exactly as
useState— no other name will work
This is different from how you might import React itself:
import React from 'react';
import { useState } from 'react';
The first line imports the default export (no braces needed). The second imports a named export (braces required).
Where the Import Goes
Always place your imports at the top of the file, before your component function:
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
// ... rest of component
}
Key Takeaway: useState must be imported using curly braces as a named export: import { useState } from 'react'; — this tells JavaScript to grab that specific function from the React library.