Challenge 7
by Andrew Nguyenยท Module 07 - Forms and Validation

Introduction
Not Attempted
NameProgress
Course outline
Not Read
Module 01 - Diving Into React
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Pet Parade
Not Read
Challenge 1
Not Attempted
Quiz 1
Not Attempted
Module 02 - Components and Props
Not Attempted
NameProgress
Project: Profile Card Creator
Not Read
Key Concepts
Not Read
Challenge 2
Not Attempted
Quiz 2
Not Attempted
Module 03 - State and Effect
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Magic Garden
Not Read
Challenge 3
Not Attempted
Quiz 3
Not Attempted
Module 04 - Conditional Rendering
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Weather Wardrobe
Not Read
Challenge 4
Not Attempted
Quiz 4
Not Attempted
Module 05: Handling Events
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Interactive Quiz App"
Not Read
Challenge 5
Not Attempted
Quiz 5
Not Attempted
Module 06 - Lists and Keys
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Photo Gallery"
Not Read
Challenge 6
Not Attempted
Quiz 6
Not Attempted
Key Concepts
Project: "Registration Form"
Challenge 7
Quiz 7
Not Attempted
Module 08 - Lifting State Up & Composition vs Inheritance
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Weather Dashboard"
Not Read
Challenge 8
Not Attempted
Quiz 8
Not Attempted
0 / 470 XP

Challenge: Display a Summary of the Registration Data Before Submission

Overview:

In this challenge, you will enhance the "Registration Form" project by adding a feature to display a summary of the registration data before the final submission. This will help you practice managing state, conditional rendering, and enhancing user experience.

Objective:

  • Learn how to manage and display a summary of form data.
  • Practice conditional rendering and improving user experience in React.

Instructions for the Interactive Code Block:

Step 1: Create a State Variable for Preview Mode

Create a new state variable to manage whether the form is in preview mode or not. This will help you toggle between the form view and the summary view.

Step 2: Implement a Function to Toggle Preview Mode

Create a function that toggles the <strong>isPreview</strong> state. This function will be called when the user clicks the "Preview" button to switch to the summary view.

Step 3: Add a "Preview" Button

Add a "Preview" button to your form. When clicked, this button should trigger the <strong>togglePreview</strong> function to display the summary of the form data.

Step 4: Conditionally Render the Form or Summary

Use conditional rendering to display either the form or the summary based on the <strong>isPreview</strong> state. If <strong>isPreview</strong> is <strong>true</strong>, display the summary of the form data; otherwise, display the form.

Step 5: Display the Summary of Form Data

In the summary view, display the values of the form fields (<strong>username</strong>, <strong>email</strong>, <strong>password</strong>, <strong>confirmPassword</strong>). Provide an option to go back and edit the form if needed.

Next Steps:

  • Improve the User Interface: Add styles to the summary view to make it visually appealing.
  • Form Validation: Ensure that all validation errors are resolved before allowing the user to switch to the preview mode.
  • Confirmation Message: Display a confirmation message or redirect the user after successful registration.

By completing this challenge, you will enhance your Registration Form project with a user-friendly summary view, allowing users to review and confirm their information before submission. This practice will help you improve state management and conditional rendering in React. Enjoy enhancing your form!


Hint Step 1: Use the <strong>useState</strong> hook to create a boolean state variable, e.g., <strong>isPreview</strong>.

// Create a state variable to manage preview mode
const [isPreview, setIsPreview] = useState(false);

Hint Step 2: The function should toggle the value of <strong>isPreview</strong> between <strong>true</strong> and <strong>false</strong>.

// Function to toggle preview mode
function togglePreview() {
  setIsPreview(!isPreview);
}

Hint Step 3: Place the button inside the form but outside of any form fields.

// Add a Preview button to toggle preview mode
&lt;button type="button" onClick={togglePreview}&gt;Preview&lt;/button&gt;

Hint Step 4: Use a ternary operator or <strong>if-else</strong> statement for conditional rendering.

// Conditionally render the form or summary
return (
  &lt;div&gt;
    {isPreview ? (
      // Render summary of form data here
    ) : (
      &lt;form onSubmit={handleSubmit}&gt;
        {/* Form fields go here */}
        &lt;button type="button" onClick={togglePreview}&gt;Preview&lt;/button&gt;
        &lt;button type="submit"&gt;Register&lt;/button&gt;
      &lt;/form&gt;
    )}
  &lt;/div&gt;
);

Hint Step 5: Use a simple div or list to display the form field values and a button to toggle back to the form view.

// Render summary of form data
&lt;div&gt;
  &lt;h3&gt;Review Your Information&lt;/h3&gt;&lt;p&gt;Username: {username}&lt;/p&gt;&lt;p&gt;Email: {email}&lt;/p&gt;&lt;p&gt;Password: {password.replace(/./g, '*')}&lt;/p&gt; {/* Mask the password */}
  &lt;p&gt;Confirm Password: {confirmPassword.replace(/./g, '*')}&lt;/p&gt; {/* Mask the password */}
  &lt;button type="button" onClick={togglePreview}&gt;Edit&lt;/button&gt;
  &lt;button type="submit" onClick={handleSubmit}&gt;Confirm and Register&lt;/button&gt;
&lt;/div&gt;
Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline