Skip to content

Add React 19 support #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tasty-mice-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"usehooks-ts": major
"www": minor
---

Add React 19 support
142 changes: 0 additions & 142 deletions apps/www/src/app/(docs)/migrate-to-v3/page.tsx
Copy link

Choose a reason for hiding this comment

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

@juliencrn may disagree, but I think it'd be best to leave this here. The process to go from v2 to v3 doesn't change with this PR and there may be older projects still using v2 that can't get upgrade to react@19 so there's no need to get rid of the docs.

Copy link
Author

@iwan-uschka iwan-uschka Feb 8, 2025

Choose a reason for hiding this comment

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

Sounds reasonable to me. It doesn't do any harm to keep valuable information. @juliencrn any thoughts on this one?

This file was deleted.

27 changes: 27 additions & 0 deletions apps/www/src/app/(docs)/migrate-to-v4/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PageHeader } from '@/components/docs/page-header'
import { components } from '@/components/ui/components'

export default async function MigrateToV4Page() {
return (
<main className="relative py-6 lg:gap-10 lg:py-10 xl:grid xl:grid-cols-[1fr_300px]">
<div className="mx-auto w-full min-w-0">
<PageHeader
id="migrate-to-v4"
className="scroll-m-20"
heading={'Migrate to v4'}
/>
<components.p>
<components.code>usehooks-ts</components.code> bumped to version 4 and
it&apos;s a major release to support{' '}
<components.a
href="https://react.dev/blog/2024/12/05/react-19"
target="_blank"
>
React 19
</components.a>
. Just install v4 and you&apos;re good to go.
</components.p>
</div>
</main>
)
}
4 changes: 2 additions & 2 deletions apps/www/src/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const docsConfig: DocsConfig = {
href: '/introduction',
},
{
title: 'Migrate to v3',
href: '/migrate-to-v3',
title: 'Migrate to v4',
href: '/migrate-to-v4',
},
],
},
Expand Down
6 changes: 3 additions & 3 deletions packages/usehooks-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
"@testing-library/react": "^14.2.1",
"@types/lodash.debounce": "^4.0.9",
"@types/node": "^20.11.19",
"@types/react": "18.2.73",
"@types/react": "19.0.8",
"eslint-config-custom": "workspace:*",
"eslint-plugin-jsdoc": "^48.1.0",
"eslint-plugin-tree-shaking": "^1.12.1",
"jsdom": "^24.0.0",
"react": "18.2.0",
"react": "19.0.0",
"react-dom": "19.0.0",
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"vitest": "^1.3.1"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function Component() {
.then(() => {
console.log('Copied!', { text })
})
.catch(error => {
.catch((error: unknown) => {
console.error('Failed to copy!', error)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export function useDebounceCallback<T extends (...args: any) => ReturnType<T>>(
delay = 500,
options?: DebounceOptions,
): DebouncedState<T> {
const debouncedFunc = useRef<ReturnType<typeof debounce>>()
const debouncedFunc = useRef<ReturnType<typeof debounce> | undefined>(
undefined,
)

useUnmount(() => {
if (debouncedFunc.current) {
Expand Down
4 changes: 2 additions & 2 deletions packages/usehooks-ts/src/useEventListener/useEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function useEventListener<
handler:
| ((event: HTMLElementEventMap[K]) => void)
| ((event: SVGElementEventMap[K]) => void),
element: RefObject<T>,
element: RefObject<T | null>,
options?: boolean | AddEventListenerOptions,
): void

Expand Down Expand Up @@ -88,7 +88,7 @@ function useEventListener<
| MediaQueryListEventMap[KM]
| Event,
) => void,
element?: RefObject<T>,
element?: RefObject<T | null>,
options?: boolean | AddEventListenerOptions,
) {
// Create a ref that stores handler
Expand Down
2 changes: 1 addition & 1 deletion packages/usehooks-ts/src/useHover/useHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useEventListener } from '../useEventListener'
* ```
*/
export function useHover<T extends HTMLElement = HTMLElement>(
elementRef: RefObject<T>,
elementRef: RefObject<T | null>,
): boolean {
const [value, setValue] = useState<boolean>(false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export function useIntersectionObserver({
entry: undefined,
}))

const callbackRef = useRef<UseIntersectionObserverOptions['onChange']>()
const callbackRef =
useRef<UseIntersectionObserverOptions['onChange']>(undefined)

callbackRef.current = onChange

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type EventType =
* ```
*/
export function useOnClickOutside<T extends HTMLElement = HTMLElement>(
ref: RefObject<T> | RefObject<T>[],
ref: RefObject<T | null> | RefObject<T | null>[],
handler: (event: MouseEvent | TouchEvent | FocusEvent) => void,
eventType: EventType = 'mousedown',
eventListenerOptions: AddEventListenerOptions = {},
Expand All @@ -41,7 +41,7 @@ export function useOnClickOutside<T extends HTMLElement = HTMLElement>(
const target = event.target as Node

// Do nothing if the target is not connected element with document
if (!target || !target.isConnected) {
if (!target?.isConnected) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Size = {
/** The options for the ResizeObserver. */
type UseResizeObserverOptions<T extends HTMLElement = HTMLElement> = {
/** The ref of the element to observe. */
ref: RefObject<T>
ref: RefObject<T | null>
/**
* When using `onResize`, the hook doesn't re-render on element size changes; it delegates handling to the provided callback.
* @default undefined
Expand Down
Loading