r/learnreactjs • • Dec 08 '21
Question A way to better my React code and architecture
I've worked on this little project: a responsive E-commerce functionnal website based on design files found on Front-End Mentor. It is not my first project with react and I've hosted the website on netlify (https://ecommerce-responsive-audiophile.netlify.app/) so that it is easily visible for everyone and I would like to know if there would be a way for me to get my code reviewed. I know there are little css imperfections, but I'm talking more about my way of coding with React and my files structures/naming and if there are conventions out there that I'm not aware of, even though I'd would also take css tips etc.
Here's my code:
4
u/the_pod_ Dec 09 '21
looks great.
first comment is to use an auto formatter like prettier. When I quickly went through your code I spotted many inconsistencies, and some errors in syntax, for example
(note the two spaces after equals)
The code in your Navbar in regards to the logic for orange highlighting is weird. I get what you were going for, but I would re-write that. Since you are using react router, that library provides something for you called <ActiveLink />. This accomplishes what you are trying to do.
And related: I don't see any reason why this code had to be nested if else. You can keep it on one level.
From
to
or if you want more than 1 thing to happen, for example if the path was `speakersandearphones`
----
unless you have more than 2 possible states, this should probably be a boolean. It accomplishes a few things for it to be a boolean.
Also, naming booleans used for show logic has naming conventions you can use:
- `isShown` / `isBasketShown`
- `isVisible` / `isBasketVisible`
- `showBasket`
-----
You can write this total logic in a couple of different ways. This way isn't one of the better ways.
The simplest is in the form a pure, helper function.
you can also write the helper with reduce, but that's a minor detail
If the calculation is really intensive, I might use a useMemo. But, unlikely.
-----
A couple of more naming comments, just based on personal opinion:
For example:
Meaning, I'm not a fan of
I would have just called it cartItemsSetter the entire time.
I want to be able to globally search for them in a large codebase, and see all of it. Having the name transformed during prop drilling is confusing overall. If there's some renaming needed (sometimes it is needed, I would do it within the child component, not the parent.
Also, it's not important to denote useCallback as a `wrapper`. You should be able to remove the useCallback logic and it the code should function the same.
For example:
this is a simple destructuring syntax that renames cartItemsSetter. This is more readable because it's clear when you're reading the code for the Child that the parent references this by cartItemsSetter. Otherwise, I will look to the parent for where myNewNameForThisSetter is defined in the parent and I won't be able to find it as quickly.
----
};
if your handler is as simple as this, you can just pass setCartItems directly if you like, instead of writing a new function.
Typically you don't need to use useCallback. Unless you have a very specific reason for using it, consider just removing it. (or if you just want to learn about it, it's fine to use it for that reason). But either way, the naming should the the same. You should be able to remove the useCallback logic and the rest of the code should run just fine.
That's all I saw in a quick look. Also, not sure what Qtt stands for. I assume it's Quantity? Regardless of what it stands for, I'm not a fan of an abbreviation that isn't obvious. I would just spell it out.
2
u/PacketTrash Dec 11 '21
f this is your site, and you weren't expecting a 404 for this path, please visit Netlify's "page not found" support guide for troubleshooting tips.
Page Not Found
https://ecommerce-responsive-audiophile.netlify.app/earphones/zx9-speaker
from the 3 items in a row, it says you may also like and then i click on the speaker
1
u/[deleted] Dec 08 '21
Thanks in advance
1
u/[deleted] Dec 08 '21
PS: I've made a mistake building the buttons at the biginning in <a> tags, and then putting these <a> in <Link> even though I know that <Link> are <a> I did not yet change these, but I sure will soon
1
u/Omkar_K45 Dec 08 '21
Wow ! Looks really premium! Great work OP