Home > front end >  Passing a variable from one component to another
Passing a variable from one component to another

Time:07-04

I have a quiz app built with React, that after the user answers it, it shows his score and then he needs to insert his email to send the results to the site owner.

I have a main component called App.js with the quiz and I saved his score like this:

const[score,setScore] = useState(0);

Now I want to show this score in my component "Score.js", so I need to get this score from App.js and send it to Score.js.

CodePudding user response:

It depends on the component structure. If they are sibling components, you can create/have the state in their parent component. If App component is top-level, you can move score state to that level and pass it to low-level components such as Score and Quiz as a prop.

CodePudding user response:

I would highly recommend researching how props work in react.

You can pass them down to a child component or maybe you will need to move the state to the next common ancestor component.

  • Related