Skip to content

Commit

Permalink
Lesson 26 - STATE
Browse files Browse the repository at this point in the history
  • Loading branch information
fmcarvalho committed Nov 8, 2024
1 parent 81c3969 commit 3f7eab2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lesson26-react-state/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<script src="bundle.js" type="module"></script>
</head>
<body>
<h1>Demo JS and React and DOM</h1>
<h3>Demo React and STATE</h3>
<div id="container"></div>
</body>
</html>
42 changes: 42 additions & 0 deletions lesson26-react-state/src/demo04-state-counter-WRONG.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react'
import * as ReactDom from 'react-dom/client'

/*
* DON'T DO THIS:
* - Don't use MUTABLE state!!!!!
* - Don't capture MUTABLE state from a Component Funcion with CLOSURE
*/
let count = 0

function Counter() {
function incrementHandler() {
count++
myRender() // !!!!! DON'T DO THIS !!!! DO NOT CALL RENDER - NEVER
}
function decrementHandler() {
count--
myRender() // !!!!! DON'T DO THIS !!!! DO NOT CALL RENDER - NEVER
}
return (
<div>
<button onClick={decrementHandler}>-</button>
<b>{count}</b>
<button onClick={incrementHandler}>+</button>
</div>
)
}

const root = ReactDom.createRoot(document.getElementById('container'))

function myRender() {
root.render(
<div>
<Counter />
<Counter />
</div>
)
}

export function demo04() {
myRender()
}
37 changes: 37 additions & 0 deletions lesson26-react-state/src/demo05-state-counter-hooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react'
import * as ReactDom from 'react-dom/client'

function Counter() {
const [count, setCount] = React.useState(0)
const [label, setLabel] = React.useState("even")
function incrementHandler() {
setCount(count + 1)
}
function decrementHandler() {
setCount(count - 1)
}
return (
<div>
<button onClick={decrementHandler}>-</button>
<b>{count}</b>
<button onClick={incrementHandler}>+</button>
{/* Show the value of State label with the even or odd according to the count */}
</div>
)
}

const root = ReactDom.createRoot(document.getElementById('container'))

function myRender() {
root.render(
<div>
<Counter />
<Counter />
<Counter />
</div>
)
}

export function demo05() {
myRender()
}
6 changes: 4 additions & 2 deletions lesson26-react-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// import { demo01 } from "./demo01-intro-react"
// import { demo02 } from "./demo02-reconciliation"
import { demo03 } from "./demo03-components"
// import { demo03 } from "./demo03-components"
import { demo04 } from "./demo04-state-counter-WRONG";
import {demo05} from "./demo05-state-counter-hooks"

demo03()
demo05()

0 comments on commit 3f7eab2

Please sign in to comment.