Lesson 23 of 3077%

(Progress persistence is disabled until Phase 9)

React
Beginner

Dynamic UI

Putting state, events, and list rendering together.

Let’s build a dynamic Todo list!

function TodoApp() {
  const [todos, setTodos] = React.useState(["Learn React"]);

  function addTodo() {
    // We create a NEW array containing all old todos, plus the new one.
    // NEVER mutate the existing array!
    setTodos([...todos, "New Task"]);
  }

  return (
    <div>
      <button onClick={addTodo}>Add Task</button>
      <ul>
        {todos.map((todo, index) => (
          <li key={index}>{todo}</li>
        ))}
      </ul>
    </div>
  );
}

(Note: We used the index as a key here for simplicity, but a unique ID is better!)

Try It Yourself

Lesson Progress

Practice This TopicTake QuizBuild a React Project

Ready to practise?

Apply what you learned with a hands-on challenge.

Practise This Topic