I am new on React.js and I don't understand, how I should pass the score children to app parent, and reset all components score via "Reset Components Score" on the app parent button, I try everything noting work, thanks for the help!
Player children components:
import React, { useState } from 'react';
function Player(props) {
const [score, setScore] = useState(0);
function scoreUp(params) {
setScore(score + 1);
}
function scoreDown(params) {
score <= 0 ? setScore(0) : setScore(score - 1);
}
const scoreReset = () => {
setScore(0);
};
return (
<div>
<center>
<h1>{props.name}</h1>
<h2>{score}</h2>
<button style={{ backgroundColor: 'green' }} onClick={scoreUp}>
+
</button>
<button style={{ backgroundColor: 'red' }} onClick={scoreDown}>
-
</button>
<button style={{ backgroundColor: 'purple' }} onClick={scoreReset}>
Reset {props.name} Score
</button>
</center>
</div>
);
}
export default Player;
App Parent:
import React from 'react';
import Player from './Player';
function App() {
return (
<div>
<Player name='Benjamin' />
<Player name='Alex' />
<button style={{ backgroundColor: 'black' }}>
Reset Components Score
</button>
</div>
);
}
export default App;