Objective:
- Understand state management in React using the
useState
hook.
- Learn how to handle events, lift state up, and implement conditional rendering.
Exercises:
- Exercise 1: Basic Counter with
useState
- Create a new component called
Counter.jsx
.
- Inside
Counter
, use the useState
hook to create a piece of state called count
initialized to 0
.
- Render the current value of
count
and a button that, when clicked, increments the count
by 1.
- Import and use the
Counter
component in App.jsx
to display it on the page.
- Exercise 2: Handling Input Events
- Create a new component called
TextInput.jsx
.
- Use
useState
to manage the value of an input field.
- Render an input field and display the current value of the input below it.
- Update the state whenever the input value changes.
- Import and use the
TextInput
component in App.jsx
.
- Exercise 3: Form Handling
- Create a component called
SimpleForm.jsx
with the following features:
- An input field for a user's name.
- A submit button.
- Use
useState
to store the input value.
- When the form is submitted, display the entered name below the form.
- Prevent the default form submission behavior using
event.preventDefault()
.