-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21dd228
commit a52501a
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
import { useCounter } from '@/hooks/useCounter' | ||
import { useComputed } from '@/hooks/useComputed' | ||
|
||
export function ComputedExample() { | ||
const [count, increment] = useCounter(0) | ||
const doubled = useComputed(() => count * 2, [count]) | ||
const [firstName, setFirstName] = useState('John') | ||
const [lastName, setLastName] = useState('Doe') | ||
const fullName = useComputed( | ||
() => `${firstName} ${lastName}`, | ||
[firstName, lastName], | ||
) | ||
|
||
return ( | ||
<article> | ||
<h2>Computed</h2> | ||
<p>Count: {count}</p> | ||
<p>Doubled: {doubled}</p> | ||
<div className="flex"> | ||
<button className="btn-secondary" onClick={() => increment()}> | ||
Increment | ||
</button> | ||
</div> | ||
<div className="flex flex-col"> | ||
<label> | ||
First name | ||
<input | ||
className="input" | ||
value={firstName} | ||
onChange={(e) => setFirstName(e.target.value)} | ||
/> | ||
</label> | ||
<label> | ||
Last name | ||
<input | ||
className="input" | ||
value={lastName} | ||
onChange={(e) => setLastName(e.target.value)} | ||
/> | ||
</label> | ||
<p>Full name: {fullName}</p> | ||
</div> | ||
</article> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export function useComputed<T>(fn: () => T, deps: unknown[] = []) { | ||
const [value, setValue] = useState<T>(fn()) | ||
|
||
useEffect(() => { | ||
setValue(fn()) | ||
}, deps) | ||
|
||
return value | ||
} |
a52501a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
games – ./apps/games
games.emiljohansson.dev
games-ivory.vercel.app
games-emiljohansson.vercel.app
games-git-main-emiljohansson.vercel.app
a52501a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
emiljohansson.dev – ./apps/next
emiljohanssondev-emiljohansson.vercel.app
emiljohanssondev-git-main-emiljohansson.vercel.app
emiljohansson.dev