Show the Score چالش 1 از جلسهی 21، آسان
امتیاز در App نگه داشته میشود، ولی Board همیشه مینویسد Score: 0.
امتیاز را با prop به Board بده (score={score}) و در Board همان را نشان بده.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<div id="root" dir="ltr"></div>
<script type="text/jsx">
import { useState } from "react";
import { createRoot } from "react-dom/client";
function Board() {
return <p id="board">Score: 0</p>;
}
function App() {
const [score, setScore] = useState(0);
return (
<div>
<Board />
<button id="add" onClick={() => setScore(score + 1)}>+1</button>
</div>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 21: state را بالا ببر · چالش بعدی: Two Viewers
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js