+ 1

How to pass prop from child component to child component.

Have to pass setScore function as prop from Nav.js to App.js to change the score value. App.js import logo from './logo.svg'; import './App.css'; import Nav from "./Component/nav.js"; function App() { return ( <div className = "App"> <div className = "nav"> <Nav /> </div> <button onClick={setScore(score + 1)}>Increment</button> </div> ); } export default App; ////////////////////////////// Nav.js import {useState} from 'react' import './Style Components/nav.css'; function Nav(){ const [score, setScore] = useState(0); return ( <div className = "nav-container"> <div className="score" id="score"> {score} {console.log(score)} </div> </div> ); } export default Nav;

21st Jan 2021, 4:08 AM
Aman Kumar
Aman Kumar - avatar
4 Answers
+ 5
App.js import React, {useState} from "react"; import logo from './logo.svg'; import './App.css'; import Nav from "./Component/nav.js"; function App() { const [score, setScore] = useState(0); return ( <div className = "App"> <div className = "nav"> <Nav score={score} /> </div> <button onClick={() => setScore(s=>s+1)}>Increment</button> </div> ); } export default App; ////////////////////////////// Nav.js import {useState} from 'react' import './Style Components/nav.css'; function Nav({score}){ return ( <div className = "nav-container"> <div className="score" id="score"> {score} </div> </div> ); } export default Nav;
21st Jan 2021, 5:16 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 1
Will you tell where have you learnt react js from.
21st Jan 2021, 2:27 PM
Aman Kumar
Aman Kumar - avatar
+ 1
Thanks
22nd Jan 2021, 3:30 AM
Aman Kumar
Aman Kumar - avatar