I'm practicing JavaScript and React by creating a quiz that will go through 5 questions, show your total score at the end, and show the high scores of those taking the quiz. I'm in the process of getting the "start quiz" button to show the first question when you click on it, but it won't unless the start screen is rendered twice. My problem is kind of hard to describe so bear with me.
This is my app.js file
import React, { useState } from 'react'
import "./style.css";
import Home from "./components/Home";
import QuestionOne from './components/QuestionOne';
import End from "./components/End";
import HighScores from "./components/HighScores";
// this is the home screen before you start the quiz
const App = () => {
// default page is Home and we change the page when the start button is clicked
const [currentPage, setCurrentPage] = useState("Home");
// this checks which page it is and returns the proper page
const renderPage = () => {
switch (currentPage) {
case "Home":
return <Home />;
case "QuestionOne":
return <QuestionOne />;
case "End":
return <End />;
case "HighScores":
return <HighScores />;
default:
return <Home />;
}
}
// home page
return (
<div>
{/* change this to see whichever page until I can get the onClick down correctly*/}
<Home currentPage={currentPage} setCurrentPage={setCurrentPage}/>
{renderPage()}
</div>
);
}
export default App;
This is my Home.js file
import React from 'react'
const Home = ({ setCurrentPage }) => {
return (
<section className="section" id="start">
<div className="container-fluid text-center">
<div className="row row-cols-2">
<div className="col text-start" id="highScores">View High Scores</div>
<div className="col text-end">Time</div>
</div>
<div className="col text-center align-self-center" id="quizTitle"><strong>Coding Quiz Challenge</strong></div>
<div className="col text-center align-self-center" id="quizExplain">Try to answer the following code-related questions within the time limit. <br></br>
Keep in mind that incorrect answers will penalize your score/time <br></br>
by ten seconds</div>
<button
href="#home"
type="button"
onClick={() => setCurrentPage("QuestionOne")}
id="startQuiz">Start Quiz
</button>
</div>
</section>
)
}
export default Home
I'm stuck with these 2 lines in particular
<Home currentPage={currentPage} setCurrentPage={setCurrentPage}/>
{renderPage()}
The start page will currently show up twice, with 2 "start quiz" buttons. When I click the first "start quiz" button rendered with , the SECOND "start quiz" button rendered with {renderPage()} will change to the first question, but the first "start quiz" button and info will stay the same. Here is an imgur album showing what I mean: https://i.sstatic.net/7R3Wx.jpg
If I take out {renderPage()} and just leave it will only show one "start quiz" screen, but the "start quiz" button will not do anything. No errors or anything. If I take out , it tells me that "setCurrentPage" is not a function. I know it isn't a function because it doesn't know where "setCurrentPage" is coming from. There are no props being passed to it.
I've been stuck on this for weeks. Someone please help lol
renderPagereturns<Home />so you're effectively running<Home currentPage={currentPage} setCurrentPage={setCurrentPage}/> <Home />, thus showing it twice.<Home />, you're updating thecurrentPagevalue toQuestionOnewhich causes a re-render. So nowrenderPage()will go through it'sswitchstatement and see the new value forcurrentPage, and thus rendering<QuestionOne />. This is why you see the second rendering update, but not the first, as the first render of the<Home />component is just, well, the Home page.