Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Newsletter sign up with React

Mari 535

@ChurrinChurron

Desktop design screenshot for the Newsletter sign-up form with success message coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


Any suggestions for improving the React code would be appreciated. Thanks!

Community feedback

StarWolf 260

@BParadowski

Posted

Hey,

after taking a look at your code it looks like you are writing Vanilla JS in React. Using DOM API like querySelector() in React is generally a last resort. Since you are bringing in a UI framework you should actually use it.

The main problem with your implementation, from a conceptual standpoint, is that you have 2 sources of truth governing what gets rendered - React and Css classes. Let's take a look at your e-mail validation error message:

<div className='form__text'>
  <label htmlFor="email">Email address</label>
  <span>Valid email required</span>
</div>

You trigger the visibility of this div by changing the class. In React, this same functionality should be achieved by declaring an 'isErrorVisible" boolean variable using useState() and changing the code as follows:

 {isErrorVisible && (
        <div className="form__text">
          <label htmlFor="email">Email address</label>
          <span>Valid email required</span>
        </div>
  )}

And of course changing the state in your submit function. You have only one class on your element this way and no reason to query any selectors. (You can also conditionally trigger classes based on the state if you really need it)

Hope this helped

Marked as helpful

1

Mari 535

@ChurrinChurron

Posted

Thanks! I'm new with React, and these suggestions are helpful for me. :D @BParadowski

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord