# Step 1.6 - Creating a state-driven UI (Demo) In React, the data travels in one direction: top-down in the form of state propagating down the component hierarchy. Only the component containing the state can change the state itself. When a UI interaction occurs, a stateful component must pass down an event handler to the UI component triggering the event in order to signal a state change. [Step #3 of "Thinking in React"](https://reactjs.org/docs/thinking-in-react.html#step-3-identify-the-minimal-but-complete-representation-of-ui-state) suggests finding the "minimal set of mutable state" that your application requires. What pieces of state can we identify? [Step #4 of "Thinking in React"](https://reactjs.org/docs/thinking-in-react.html#step-4-identify-where-your-state-should-live) asks us to think about where our state should live. - Is the state local to a single component? - Is the state derived from another state? - Is the state primarily in one component but shared with others? - Is the state global? ## Adding state to TodoApp Inside of our `TodoApp` component we only need to track two pieces of state, our `todos` and the current `filter`. We don't need to worry about a `remaining` count because it can be calculated by counting the number of todos where `status` is set to `active`. ```jsx export const TodoApp = () => { const [filter, setFilter] = React.useState('all'); const [todos, setTodos] = React.useState([ { id: '04', label: 'Todo 4', status: 'completed', }, { id: '03', label: 'Todo 3', status: 'active', }, { id: '02', label: 'Todo 2', status: 'active', }, { id: '01', label: 'Todo 1', status: 'active', }, ]); ``` ## Passing state through to UI Now we can pass `filter` and `todos` into our components. ```jsx return (
); ``` ## State-driven TodoList I've already pulled out our props into `filter` and `todos` variables, and written a bit of JS that will return an array of filtered todos. We'll be using that filtered array to render our todo items. React requires any dynamic length list to have unique `key` properties, for which we can use the `todo.id`. This key helps React to only re-render the parts of the list that changes. ```jsx return ( ); ``` ## State-driven and stateful TodoHeader In `TodoHeader.tsx` we are going to both display the selected filter state, and track the text for a new todo. ### Conditional class names In CSS-based styling, visual states are applied by adding and removing classes. We can use the filter value to conditionally add a class, thereby lighting up the correct filter button. ```jsx ``` > The [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) `condition ? ifTrue : ifFalse` is often used to conditionally render a string or JSX element. In the case that the condition is false the `className` is simply ommited. ### Adding a controlled input In React, form elements such as ``, `