Skip to content

Commit

Permalink
Component lifecycle useEffect example and exercise implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilGidwani committed Apr 28, 2024
1 parent 370116d commit 883b433
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 80 deletions.
6 changes: 2 additions & 4 deletions src/App.jsx
@@ -1,9 +1,7 @@
import { ClassComponent } from "./ClassComponent"
import { FunctionComponent } from "./FunctionComponent"
import { Child } from "./Child"

function App() {
return <FunctionComponent />
//return <ClassComponent />
return <Child />
}

export default App
30 changes: 30 additions & 0 deletions src/Child.jsx
@@ -0,0 +1,30 @@
import { useEffect, useState } from "react"

export function Child() {
const [name, setName] = useState("")
const [age, setAge] = useState(0)

//useEffect example
/* useEffect(() => {
console.log("Name or age was changed", name, age)
}, [name, age]) */

//useEffect Exercise
useEffect(() => {
console.log("Age has changed", age)
}, [age])

return (
<div>
<input value={name} onChange={(e) => setName(e.target.value)} />
<br />
<br />
<button onClick={() => setAge((currentAge) => currentAge - 1)}>-</button>
{age}
<button onClick={() => setAge((currentAge) => currentAge + 1)}>+</button>
<p>
My name is {name} and I am {age} years old
</p>
</div>
)
}
39 changes: 0 additions & 39 deletions src/ClassComponent.jsx

This file was deleted.

32 changes: 0 additions & 32 deletions src/FunctionComponent.jsx

This file was deleted.

Binary file removed src/assets/counter-with-name-project-snapshot.jpg
Binary file not shown.
6 changes: 1 addition & 5 deletions src/main.jsx
Expand Up @@ -2,8 +2,4 @@ import React from "react"
import ReactDOM from "react-dom/client"
import App from "./App.jsx"

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
ReactDOM.createRoot(document.getElementById("root")).render(<App />)

0 comments on commit 883b433

Please sign in to comment.