
Example of state
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
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:
/* src/Component.js */
import { useState } from 'react'
Then, we define state variables and state setter functions by de-structuring the useState hook:
/* src/Component.js */
import { useState } from 'react'
export default function Component() {
const [ title, setTitle ] = useState('')
return (
<div>
<h1>{title}</h1>
</div>
)
}
Note:
the state variable,
title, can actually have any legal JavaScript variable namethe 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 whichdescribes the state's initial value
can be any primitive (string, number, boolean, object, array)
the state gets referenced in the
returnstatement like any other variable in JSX with the curly braces
Updating the state in React
As mentioned in the previous section, 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:
/* src/Component.js */
import { useState } from 'react'
export default function Component() {
const [ title, setTitle ] = useState('hello')
const handleClick = () => {
setTitle('goodbye')
}
return (
<div>
<h1>{title}</h1>
<button onClick={handleClick}>change title</button>
</div>
)
}
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 events
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:
/* src/NewComponent.js */
import { useState } from 'react'
export default function NewComponent() {
const [ title, setTitle ] = useState('hello')
const handleChange = (event) => {
setTitle(event.target.value)
}
return (
<div>
<h1>{title}</h1>
<label htmlFor="custom">Customize name: </label>
<input type="text" name="custom" onChange={handleChange} />
</div>
)
}
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 JavaScriptwe used the
handleChangefollowing the naming convention in the previous examplethe
handleChangetakes on a parametereventsupplied by the input fieldevent.targetrefers to the input field whileevent.target.valuerefers to the text in that field
We can see this action with a CodeSandbox:
Code repo
available on https://github.com/joncoded/jonotype/tree/002-state


