Hi guys I am trying to call my child component: MakeQuizePage
in my parent component: ‘StartPage’
but not works my code :
import MakeQuizePage from './makeQuizePage';
function StartPage() {
return (
<>
<h1 className=" my-5 fs-1 text-danger text-center ">کی از همه خبیث تره</h1> ;
<div className="my-5 text-center">
<button onClick={nextPage} className="shadow btn p-4 btn-lg col-3 text-center">start </button>
</div>
</>
)
function nextPage() {
return (<MakeQuizePage />)
}
}
export default StartPage;
and this is my child component: MakeQuizePage (jsx)
import { useState } from "react";
const MakeQuizePage = () => {
const [pageCount, setPageCount] = useState(0);
const questionAndRezults = [
[
'asa'
],
[
'akks'
]
];
return (
<>
<p className='text-center'>{questionAndRezults[pageCount][0]}</p>
{questionAndRezults[pageCount].slice(1).map((p, index) => (
<>
<p className='text-center'>{questionAndRezults[pageCount].slice(1)[index]}</p>
<button className="form-check-input" type="radio"></button>
</>
))}
</>
)
const nextPageMaker = function nextPage() {
setPageCount(
pageCount + 1);
console.log(pageCount)
}
}
export default MakeQuizePage;
///when I tested by a simple function this onclick work, but not working by Codes above
4
Answers
You said your file is
MakeQuizePage.jsx
. and your importingif your folder is MakeQuizePage.jsx try importing like this
the error is in folder name.
you can check the also check what is the error in your browser console
It depends on your product requirements
MakeQuizePage
to be an independent page with its own URL that people can directly access itOR
MakeQuizePage
andStartPage
share the same URLFor #1, you should consider using React Router or NextJS as they provide you a routing mechanism for pages with different URLs
For #2, the
onClick
callback should control what is visible instead of returningMakeQuizePage
A high level code could look like this
If you want the nextPage nested in the StartPage component,you can use display to control the visiability of the nextPage,like below:
If you only want to jump to next page and replace startpage,you can do the follwing:
npm install [email protected];
Here is a demo containing the code above
You can achieve your goal using setState, here is a snippet of the Parent Component Code:
This way whenever user click the button the child component will be displayed.
Hope this helps you.