Design comparison
Solution retrospective
Any suggestions for improving the React code would be appreciated. Thanks!
Community feedback
- @BParadowskiPosted about 1 year ago
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 helpful1@ChurrinChurronPosted about 1 year agoThanks! 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 GitHubJoin 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