-
Notifications
You must be signed in to change notification settings - Fork 52
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
fix(react): add <ClientOnly/>
internally
#964
Conversation
🦋 Changeset detectedLatest commit: 4b8d354 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
import { useSyncExternalStore } from 'react' | ||
import { noop } from '../utils' | ||
|
||
function getSnapshot() { | ||
return false | ||
} | ||
|
||
function getServerSnapshot() { | ||
return true | ||
} | ||
|
||
// hook interface instead of useIsClient in hooks | ||
export function useIsClient() { | ||
return useSyncExternalStore(() => noop, getSnapshot, getServerSnapshot) | ||
} |
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.
This change is for next stage. Use as-is useIsClient please first for current user.
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.
How about this?
import React, { useState } from 'react'
import { noop } from '../utils'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
export const useIsClient = () => {
const [isClient, setIsClient] = useState(false)
useIsomorphicLayoutEffect(() => {
setIsClient(true)
}, [])
if (typeof React['useSyncExternalStore'] === 'function') {
return React['useSyncExternalStore'](
() => noop,
() => true,
() => false
)
}
return isClient
}
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.
This is a variant of the existing useIsClient
and does not change the interface of the current Suspensive
user; rather, the migration to the next stage is much more acceptable.
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.
This is a variant of the existing useIsClient and does not change the interface of the current Suspensive user; rather, the migration to the next stage is much more acceptable.
I agree your opinion and good Idea. but we should change our feature more gradually. I want to check this feature change as beta version. use original useIsClient please. In my opinion, This pull request should be just only for adding ClientOnly component. not improving Suspense clientOnly.
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.
Thank you for your opinion. I'll reflect it.
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.
Thanks for accepting my opinion. I also accept your opinion if we can too. but we should be more careful for this library users. 👍
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.
I'm just grateful to be able to contribute to a great library, with a developer I admire. If there's anything else I can contribute to, I'd love to.
export const ClientOnly = ({ children, fallback }: SuspenseProps) => { | ||
const isClient = useIsClient() | ||
|
||
return isClient ? <>{children}</> : <>{fallback}</> | ||
} |
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.
- Shortly if we can please
- This is ClientOnly. so make type ClientOnlyProps please
export const ClientOnly = ({ children, fallback }: SuspenseProps) => { | |
const isClient = useIsClient() | |
return isClient ? <>{children}</> : <>{fallback}</> | |
} | |
export const ClientOnly = ({ children, fallback }: ClientOnlyProps) => <>{useIsClient() ? children : fallback}</> |
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.
I think most of the time <ClientOnly/>
be used as the HOC, so it need a children
.
What do you think about using it as?
interface ClienOnlyProp {
children: ReactNode:
fallback?: ReactNode
}
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.
Cool! That's good for me
const SuspenseClientOnly = (props: ReactSuspenseProps) => ( | ||
<ClientOnly fallback={props.fallback}> | ||
<ReactSuspense {...props} /> | ||
</ClientOnly> | ||
) |
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.
👍
<ClientOnly/>
internally
import { render, screen } from '@testing-library/react' | ||
import { useIsClient } from '../hooks/useIsClient' | ||
import { ClientOnly } from './ClientOnly' | ||
|
||
vi.mock('./useIsClient', () => ({ | ||
useIsClient: vi.fn(), | ||
})) | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
describe('<ClientOnly/>', () => { | ||
it('renders children when isClient is true', () => { | ||
useIsClient.mockReturnValue(true) | ||
|
||
render( | ||
<ClientOnly fallback={<div>Loading...</div>}> | ||
<div>Client Content</div> | ||
</ClientOnly> | ||
) | ||
|
||
expect(screen.getByText('Client Content')).toBeInTheDocument() | ||
expect(screen.queryByText('Loading...')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('renders fallback when isClient is false', () => { | ||
useIsClient.mockReturnValue(false) | ||
|
||
render( | ||
<ClientOnly fallback={<div>Loading...</div>}> | ||
<div>Client Content</div> | ||
</ClientOnly> | ||
) | ||
|
||
expect(screen.getByText('Loading...')).toBeInTheDocument() | ||
expect(screen.queryByText('Client Content')).not.toBeInTheDocument() | ||
}) | ||
}) |
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.
Cool test code!
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.
Thanks! @Collection50
@Collection50 resolve ci:type error please. |
CodSpeed Performance ReportMerging #964 will create unknown performance changesComparing Summary
|
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.
@Collection50 Let's improve clientOnly gradually after this pull request merging
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #964 +/- ##
==========================================
+ Coverage 80.67% 80.71% +0.04%
==========================================
Files 36 37 +1
Lines 445 446 +1
Branches 98 98
==========================================
+ Hits 359 360 +1
Misses 77 77
Partials 9 9
|
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @suspensive/react@2.2.1 ### Patch Changes - [#964](#964) [`d87a482`](d87a482) Thanks [@Collection50](https://github.com/Collection50)! - fix(react): add `<ClienOnly/>` for clientOnly prop of `<Suspense/>` ## @suspensive/react-query@2.2.1 ### Patch Changes - Updated dependencies \[[`d87a482`](d87a482)]: - @suspensive/react@2.2.1 - @suspensive/react-query-4@0.0.1 - @suspensive/react-query-5@0.0.1 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
related #528 # Overview Implemented the `ClientOnly` component. ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @suspensive/react@2.2.1 ### Patch Changes - [#964](#964) [`d87a482`](d87a482) Thanks [@Collection50](https://github.com/Collection50)! - fix(react): add `<ClienOnly/>` for clientOnly prop of `<Suspense/>` ## @suspensive/react-query@2.2.1 ### Patch Changes - Updated dependencies \[[`d87a482`](d87a482)]: - @suspensive/react@2.2.1 - @suspensive/react-query-4@0.0.1 - @suspensive/react-query-5@0.0.1 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
related #528 # Overview Implemented the `ClientOnly` component. ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md) 2. I added documents and tests. ---------
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @suspensive/react@2.2.1 ### Patch Changes - [#964](#964) [`d87a482`](d87a482) Thanks [@Collection50](https://github.com/Collection50)! - fix(react): add `<ClienOnly/>` for clientOnly prop of `<Suspense/>` ## @suspensive/react-query@2.2.1 ### Patch Changes - Updated dependencies \[[`d87a482`](d87a482)]: - @suspensive/react@2.2.1 - @suspensive/react-query-4@0.0.1 - @suspensive/react-query-5@0.0.1
related #528 # Overview Implemented the `ClientOnly` component. ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @suspensive/react@2.2.1 ### Patch Changes - [#964](#964) [`d87a482`](d87a482) Thanks [@Collection50](https://github.com/Collection50)! - fix(react): add `<ClienOnly/>` for clientOnly prop of `<Suspense/>` ## @suspensive/react-query@2.2.1 ### Patch Changes - Updated dependencies \[[`d87a482`](d87a482)]: - @suspensive/react@2.2.1 - @suspensive/react-query-4@0.0.1 - @suspensive/react-query-5@0.0.1
related #528 # Overview Implemented the `ClientOnly` component. ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Jonghyeon Ko <jonghyeon@toss.im>
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @suspensive/react@2.2.1 ### Patch Changes - [#964](#964) [`d87a482`](d87a482) Thanks [@Collection50](https://github.com/Collection50)! - fix(react): add `<ClienOnly/>` for clientOnly prop of `<Suspense/>` ## @suspensive/react-query@2.2.1 ### Patch Changes - Updated dependencies \[[`d87a482`](d87a482)]: - @suspensive/react@2.2.1 - @suspensive/react-query-4@0.0.1 - @suspensive/react-query-5@0.0.1
related #528
Overview
Implemented the
ClientOnly
component.PR Checklist