Skip to content

Commit

Permalink
useEffect - dependency array
Browse files Browse the repository at this point in the history
  • Loading branch information
jessilyneh committed Mar 1, 2022
1 parent a6a5bcd commit 8a89b86
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import "./index.css";

function App() {
const [name, setName] = useState("Jess")
const [role, setRole] = useState(false)

// runs after every update and render. Promote side effect
useEffect(() => {
document.title = `celebrate ${name}`
});
console.log(`celebrate ${name}`)
},[name]);//dependency array will change the behavior everytime "name" was change

useEffect(()=> {
console.log(`this person is:${role ? "javascript developer": "student"}`)
},[role]);//everytime the console.log changes is because role changes
return(
<section>
<p>Congratulations, {name}!</p>
<button onClick={()=> setName("Rafael")}>Change winner</button>
<button
onClick={()=> setName("Rafael")}
>
Change winner
</button>
<p>{role ? "you are a developer" : "you are a student"}</p>
<button onClick={()=> setRole(true)}>become a <br/> professional developer <br/> in 18 weeks</button>
</section>
)
}
Expand Down

0 comments on commit 8a89b86

Please sign in to comment.