<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
function App() {
const [counter,setCounter] = React.useState(0);
const onClick = () =>{
setCounter(counter+1);
}
return(
<div>
<h3>클릭횟수 : {counter}</h3>
<button onClick={onClick}>누르시오</button>
</div>
);
}
ReactDOM.render(<App />,root);
</script>
</html>
const [counter,setCounter] = React.useState(0);
const onClick = () =>{
setCounter(counter+1);
}
React.useState(0)에서 받아오는 값을 (데이터값, 데이터값을 바꿔줄함수) 온클릭 이벤트에서 데이터값을 바꿔줄 함수를 지정해준다.
setCounter(counter+1);