1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
為按鈕添加value和。id
export default function App() {
const playGame = (e) => {
console.log(e.target.id);
};
return <GameContainer buttonFn={(e) => playGame(e)} />;
}
function GameContainer({ buttonFn }) {
return (
<>
<p>Game name</p>
<ButtonContainer buttonFn={buttonFn} />
</>
);
}
function ButtonContainer({ buttonFn }) {
return (
<>
<button id="btn-1" value="btn-1" onClick={buttonFn}>
X
</button>
<button id="btn-2" value="btn-2" onClick={buttonFn}>
Y
</button>
</>
);
}
代碼 - https://codesandbox.io/s/old-cloud-0deig?file=/src/App.js:51-592
添加回答
舉報(bào)