Skip to content

Commit 63ce3db

Browse files
14 - React useState
1 parent c5b6591 commit 63ce3db

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
<body>
77
<h1>Hello world</h1>
88
<div id='app'></div>
9+
<button id='myBtn'>hello btn</button>
10+
<script>
11+
var btn = document.getElementById("myBtn")
12+
btn.addEventListener('click', (event)=>{
13+
alert("Button clicked.")
14+
})
15+
</script>
916
<script src='index.js'></script>
1017
</body>
1118
</html>

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import React from 'react'
1+
import React, {useState} from 'react'
22
import ReactDOM from 'react-dom'
33

44

5+
const Btn = () =>{
6+
const [clickCount, setClickCount] = useState(0)
7+
8+
const handleClick = (event) =>{
9+
// alert("btn in react")
10+
setClickCount(clickCount + 1)
11+
}
12+
return <button onClick={handleClick} className={`class-${clickCount}`}>hello react btn</button>
13+
}
14+
515
const App = () => {
6-
return <div>Hello world again.</div>
16+
return <div>Hello world again.<Btn /></div>
717
}
818

919
const htmlElement = document.getElementById("app")

0 commit comments

Comments
 (0)