React state
// updated 2025-05-13 16:34
In React, state refers to a special variable that stores the current situation of a component, especially after the initial return render, e.g.:
- A state may contain a user's "wish list of trip destinations" for a travel app
- During the user's session, a user may add a new location to the bucket list
- Thus, the state of the wish list changes to reflect that addition
- During the user's session, a user may add a new location to the bucket list
Thus, on this page, let us learn about:
- Implementing state
- Updating the state
- Updating state with event handlers
Implementing state in React
As of now, we implement state with a React hook (a special built-in function) from the React library ... so we begin with an import statement:
1/* src/Component.jsx */
2
3import { useState } from 'react'Then, we define state variables and state setter functions by de-structuring the useState hook:
1/* src/Component.jsx */
2
3import { useState } from 'react'
4
5export default function Component() {
6
7 const [ title, setTitle ] = useState('')
8
9 return (
10 <div>
11 <h1>{title}</h1>
12 </div>
13 )
14}
15Note that:
- the state variable,
title, can actually have any legal JavaScript variable name (i.e. not a reserved keyword, begins with a letter of the alphabet) - the state setter function,
setTitle, typically follows the convention of havingsetat the beginning of its name, plus the state variable to which it refers- this typically gets used in a handler function (shown below) that gets called upon user interaction, e.g. in a form or button or "link as a button"
- the call to
useStatetypically takes on one argument which- assigns an initial value to the state variable
- can be any primitive (string, number, boolean, object, array)
- the
returnstatement can render the state variable, like any other variable, in JSX with curly braces
Updating the state in React
As mentioned in the previous subsection, we would typically update a state variable by anticipating an interaction from the user interface; that interaction would then trigger the handler function to use the state setter function:
1/* src/Component.jsx */
2
3import { useState } from 'react'
4
5export default function Component() {
6
7 const [ title, setTitle ] = useState('hello')
8
9 const handleClick = () => {
10 setTitle('goodbye')
11 }
12
13 return (
14 <div>
15 <h1>{title}</h1>
16 <button onClick={handleClick}>change title</button>
17 </div>
18 )
19}There, we added a button with an onClick prop to handleClick which calls setTitle with an argument to change the title from the initial "hello" to "goodbye"!
Note:
onClickexists a pre-determined attribute/prop name for click-type events- a
clickis the same thing astouchon mobile devices or anenterkeypress
- a
handleClickcan take on any name but we usehandleas a prefix by convention
Updating state more dynamically with event handlers
That last example showed a state update for pedagogical purposes but it might look a tad over-simplistic; let's look at another example with a user-driven input text field:
1/* src/NewComponent.jsx */
2
3import { useState } from 'react'
4
5export default function NewComponent() {
6
7 const [ title, setTitle ] = useState('hello')
8
9 const handleChange = (event) => {
10 setTitle(event.target.value)
11 }
12
13 return (
14 <div>
15 <h1>{title}</h1>
16 <label htmlFor="custom">Customize name: </label>
17 <input type="text" name="custom" onChange={handleChange} />
18 </div>
19 )
20}With that, the heading changes based on the user input (still not an amazingly cool example but shows how a seemingly static page can change with the help of state!)
Note:
- an
onChangeevent triggers the handlerhandleChange <label>tags must use the attributehtmlForinstead offorto avoid confusion with the keywordforin JavaScript- we used the
handleChangefollowing the naming convention in the previous example- the
handleChangetakes on a parametereventsupplied by the input field event.targetrefers to the input field whileevent.target.valuerefers to the text in that field
- the