--- title: Managing State --- As your application grows, it helps to be more intentional about how your state is organized and how the data flows between your components. Redundant or duplicate state is a common source of bugs. In this chapter, you'll learn how to structure your state well, how to keep your state update logic maintainable, and how to share state between distant components. * [How to think about UI changes as state changes](/learn/reacting-to-input-with-state) * [How to structure state well](/learn/choosing-the-state-structure) * [How to "lift state up" to share it between components](/learn/sharing-state-between-components) * [How to control whether the state gets preserved or reset](/learn/preserving-and-resetting-state) * [How to consolidate complex state logic in a function](/learn/extracting-state-logic-into-a-reducer) * [How to pass information without "prop drilling"](/learn/passing-data-deeply-with-context) * [How to scale state management as your app grows](/learn/scaling-up-with-reducer-and-context) ## Reacting to input with state {/*reacting-to-input-with-state*/} With React, you won't modify the UI from code directly. For example, you won't write commands like "disable the button", "enable the button", "show the success message", etc. Instead, you will describe the UI you want to see for the different visual states of your component ("initial state", "typing state", "success state"), and then trigger the state changes in response to user input. This is similar to how designers think about UI. Here is a quiz form built using React. Note how it uses the `status` state variable to determine whether to enable or disable the submit button, and whether to show the success message instead. ```js import { useState } from 'react'; export default function Form() { const [answer, setAnswer] = useState(''); const [error, setError] = useState(null); const [status, setStatus] = useState('typing'); if (status === 'success') { return

That's right!

} async function handleSubmit(e) { e.preventDefault(); setStatus('submitting'); try { await submitForm(answer); setStatus('success'); } catch (err) { setStatus('typing'); setError(err); } } function handleTextareaChange(e) { setAnswer(e.target.value); } return ( <>

City quiz

In which city is there a billboard that turns air into drinkable water?