Skip to content

Commit

Permalink
add useComputed hook
Browse files Browse the repository at this point in the history
  • Loading branch information
emiljohansson committed Jan 28, 2024
1 parent 21dd228 commit a52501a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
48 changes: 48 additions & 0 deletions apps/next/src/app/hooks/ComputedExample.tsx
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>
)
}
2 changes: 2 additions & 0 deletions apps/next/src/app/hooks/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DebounceExample } from './DebounceExample'
import { CounterExample } from './CounterExample'
import { EventListenerExample } from './EventListenerExample'
import { ClickOutsideExample } from './ClickOutsideExample'
import { ComputedExample } from './ComputedExample'

export default async function HooksPage() {
return (
Expand All @@ -18,6 +19,7 @@ export default async function HooksPage() {
<CounterExample />
<EventListenerExample />
<ClickOutsideExample />
<ComputedExample />
</article>
</Section>
</Content>
Expand Down
11 changes: 11 additions & 0 deletions apps/next/src/hooks/useComputed.ts
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
}

2 comments on commit a52501a

@vercel
Copy link

@vercel vercel bot commented on a52501a Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a52501a Jan 28, 2024

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

Please sign in to comment.