FAQ: The State Hook - Set From Previous State

This community-built FAQ covers the “Set From Previous State” exercise from the lesson “The State Hook”.

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

Learn React

FAQs on the exercise Set From Previous State

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!

Really disappointed how this course has progressed. It started off great and quickly went downhill. Went from explaining things to just throwing stuff on the wall and good luck from 0-60.

4 Likes

Hi. I don’t know if you still have issues with this part(Set From Previous State) of the State Hook lesson, but if you do, I think I know why. Hope this video helps :slight_smile: Codecademy Learn React | Lesson 5 | The State Hook - Part 4

1 Like

what helped me the most was taking some time to practice state with classes first. using setState for initialize classes before I started learning about the hooks. it does make it easier to understand how everything is happening. just go take some time to play with it in a code editor. make a few react components, practice diffrent examples and read through each line of your code and explain to yourself what each one does so you have that indepth understanding of what everything is doing.

WHat grinds me gears about this exersize isnt the material but I did the first question. passed it. then the second step wouldnt pass for no reason. I even checked the view answer, everything was identical. NOTHING was diffrent and it still wouldnt pass the step. I literally copy and pasted all the code and it still wouldnt pass it. the only way it would pass is if I clicked the replace code button. so stupid. the components were working and everything like they should. Has anyone else had this issue? everything was working great except it wouldnt let me pass the step. laaaaaammeee… im guessing it was set so senstive on this challenge that if you didnt have a space in the right place it still wasnt going to let you pass it…idk…just really frustrating.

Correct, now you see what I mean now after your post and went an experience what the rest of us saw.

Did the same thing happen to you? Yeah that kind of stuff seems to be typical with codecademy. Or even if you follow the developer video during a project to the T there’s errors or extras you have to add in.

Yes, amongst other things leading up to this. I’ve posted about this in other places with other lessons. But the point was that it was hard enough to learn how to get this right without dealing with other issues.

1 Like

did you get to he review yet at the end of the lesson where you have 3 files? if not… prepare yourself.

I just moved on. Sometimes things happen here an its best to just move on. Revisit it another time when they update it. Most courses this is OK to do unless its Ruby. That one is so bad it just needs to be deleted.

Helllo!

I have two questions regarding this exercise:

  1. on line 3, where does {questions} come from, i.e. what does it refer to ?
  2. on line 10 ( const onLastQuestion = questionIndex === questions.length - 1;) what am I seeing here?
    it’s a conditional, but without if/else or ternary operators? I’m a bit lost…

Thank you in advance for your help!

Hi could anyone please help me here -

why is there a double Parantheses is the working Callback function solution:

… setQuestionIndex((prevQuestionIndex) => prevQuestionIndex +1) …

but only a single set in the example code:

… setCount(prevCount => prevCount + 1) …

thanks!

Hi marty.
You can find the the explanation in the course “Learn JavaScript”/“Functions”/“Concise Body Arrow Function”.
For anonymous functions with one argument/parameter, it is optional to put the parameter inside parentheses.

Kind regards.

Hi.

  1. You can find the array “questions” in dataModel.js. It is being imported in App.js.
  2. It is a conditional expression. Please find other examples in the course Learning JavaScript/Conditionals/Conditional Statements/Comparison Operators.
    Here you can find an additional explanation: JavaScript Expressions and Statements

Hi everyone,

This code, behaves as I would expect it to, but for some reason, it does not pass the checkbox test. I have scoured it for typos and mistakes but can’t seem to find any. If anyone can spot any errors, I’d really appreciate it.

Thanks

Michael

import React, { useState } from 'react';

export default function QuizNavBar({ questions }) {
  const [questionIndex, setQuestionIndex] = useState(0);

  // define event handlers 
  const goBack = () => {
    setQuestionIndex(prevQuestionIndex =>
    prevQuestionIndex - 1);
  }
  // determine if on the first question or not 
  const goToNext = () => {
    setQuestionIndex(prevQuestionIndex => 
    prevQuestionIndex + 1);
  }
  const onLastQuestion = 
    questionIndex === questions.length - 1;
  const onFirstQuestion = 
    questionIndex === 0;
  
  return (
    <nav>
      <span>Question #{questionIndex + 1}</span>
      <div>
        <button 
          onClick={goBack} 
          disabled={onFirstQuestion}>
            Go Back
        </button>
        <button 
            onClick={goToNext} 
            disabled={onLastQuestion}>
              Next Question
        </button>
      </div>
    </nav>
  );
}

Nevermind,

I’ve removed the curly braces from the click handler functions so that the return values are implicit. Works now.