FAQ: The Effect Hook - Rules of Hooks

This community-built FAQ covers the “Rules of Hooks” exercise from the lesson “The Effect Hook”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn React

FAQs on the exercise Rules of Hooks

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This exercise is misleading. You can follow the steps as written and pass the checkpoints, but the code will not render anything to the preview.
After comparing with the solution, line by line, I found you need to set the initial state for items to an empty object. This is not mentioned anywhere in the instructions. The instructions only say about setting initial state for categories and selectedCategories. This caused a lot of frustration searching for a mistake that was never there - just simply poor instructions.

const [items, setItems] = useState({});
3 Likes

This lesson says that hooks should not be written inside nested functions but in the previous exercise we used stateSetter() inside the useEffect(). So is it good practice to write like this?

I understood that you can only use useState() on a top level, but once you called it, it’s okay to use part of what it returns , a.k.a. setState() inside of useEffect().

But I may be wrong.

2 Likes

yes, you are right, useState() is the hook so you need to use it on the top, but setState() isn’t a hook so you can use it anywhere except in the return cause it will cause an infinite loop in this case.

2 Likes

Made it through the first 55% of the full-stack course and enjoyed it. The hooks instructions have been difficult to understand for me though. I’ll get lost and then review the solution. Once I know the answer I try to implement the same thing, but still get a failed checkpoint. I’ve even gone so far as trying to copy and paste the solution and it rejects it whenever I input it. I’m confused as to what the instructions are looking for at this point. Specifically having difficulty with step 1 vagueness on Rules of Hooks

Perhaps you can copy/paste the code you are trying OR post a screenshot. That might offer a clue as to why you can’t progress. It may also offer clues as to what is causing you confusion.