Skip to content
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

feat(react): add useErrorBoundaryFallbackProps #216

Merged
merged 16 commits into from
Oct 18, 2023

Conversation

manudeli
Copy link
Member

@manudeli manudeli commented Oct 11, 2023

related with #200

Overview

I added useErrorBoundaryFallbackProps to make useErrorBoundary don't have role in fallback of <ErrorBoundary/>

Cause of adding useErrorBoundaryFallbackProps

  1. TypeScriptly, existing useErrorBoundary().isError can be true in children of <ErrorBoundary/>, but actually runtimely it's not.
    const Example = withErrorBoundary(() => {
      const errorBoundary = useErrorBoundary()
      errorBoundary.isError // there cannot be error, but isError can be true TypeScriptly
    }, {
      fallback: () => <></>
    })
  2. TypeScriptly, existing useErrorBoundary().isError can be false in fallback of <ErrorBoundary/>, but actually runtimely there must have error in fallback of <ErrorBoundary/>
    const Example = withErrorBoundary(() => {
      
    }, {
      fallback: function ErrorBoundaryFallback() => {
        const errorBoundary = useErrorBoundary()
        errorBoundary.isError // there must be error, but isError can be false TypeScriptly
        return <></>
      }
    })
  3. There could be useErrorBoundary().reset in children of <ErrorBoundary/>, but it didn't work in children because there is no error to reset in children of <ErrorBoundary/>.
    const Example = withErrorBoundary(() => {
      const errorBoundary = useErrorBoundary()
      errorBoundary.reset // there is reset method but this is not working for children of `<ErrorBoundary/>`
    }, {
      fallback: () => <></>
    })

Solution

I think both children / fallback of <ErrorBoundary/> should have separate hook for each section. so I made useErrorBoundaryFallbackProps separate from useErrorBoundary

  • useErrorBoundary(hook for children of <ErrorBoundary/>)
    • everytime no error
    • no reset function
    • can setError
  • useErrorBoundaryFallbackProps(hook for fallback of <ErrorBoundary/>)
    • everytime have error
    • reset function working

PR Checklist

  • I have written documents and tests, if needed.

@manudeli manudeli self-assigned this Oct 11, 2023
@changeset-bot
Copy link

changeset-bot bot commented Oct 11, 2023

🦋 Changeset detected

Latest commit: 5d72fb0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@suspensive/react Minor
@suspensive/react-query Minor

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

@vercel
Copy link

vercel bot commented Oct 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2023 9:41am
docs-legacy ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2023 9:41am
visualization ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2023 9:41am

@vercel vercel bot temporarily deployed to Preview – visualization October 11, 2023 11:20 Inactive
@manudeli manudeli changed the base branch from main to react/refactor/easier-code October 11, 2023 12:07
@manudeli manudeli changed the base branch from react/refactor/easier-code to main October 11, 2023 12:07
@codecov
Copy link

codecov bot commented Oct 11, 2023

Codecov Report

Merging #216 (5d72fb0) into main (33f6f07) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #216   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           21        21           
  Lines          829       842   +13     
  Branches       146       149    +3     
=========================================
+ Hits           829       842   +13     
Components Coverage Δ
@suspensive/react 100.00% <100.00%> (ø)
@suspensive/react-query ∅ <ø> (∅)

@vercel vercel bot temporarily deployed to Preview – visualization October 11, 2023 12:31 Inactive
@vercel vercel bot temporarily deployed to Preview – visualization October 11, 2023 12:52 Inactive
@manudeli manudeli marked this pull request as ready for review October 11, 2023 13:10
@manudeli manudeli requested a review from okinawaa as a code owner October 11, 2023 13:10
@vercel vercel bot temporarily deployed to Preview – visualization October 11, 2023 13:19 Inactive
@manudeli manudeli requested a review from minsoo-web October 14, 2023 02:01
Copy link
Member

@minsoo-web minsoo-web left a comment

Choose a reason for hiding this comment

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

I think this hook is very convenient, but how about provide props type guide like this?

import { ErrorBoundaryFallbackProps } from '@suspensive/react'

const Nested = ({ reset, error }: ErrorBoundaryFallbackProps) => {
  return (
    <>
      <button onClick={reset}>reset</button>
      {error.message}
    </>
  )
}

This is exactly same, but can't avoid props drilling.
So, my suggestion is, before provide example that can avoid props drilling case, first show simple example.


And How about add Errorboundary component's visualization page and link it?

hundred words are worth a glance. In korea 백문이불여일견

packages/react/src/ErrorBoundary.en.mdx Outdated Show resolved Hide resolved
packages/react/src/ErrorBoundary.en.mdx Outdated Show resolved Hide resolved
packages/react/src/ErrorBoundary.en.mdx Outdated Show resolved Hide resolved
packages/react/src/ErrorBoundary.en.mdx Show resolved Hide resolved
packages/react/src/ErrorBoundary.ko.mdx Outdated Show resolved Hide resolved
packages/react/src/ErrorBoundary.ko.mdx Show resolved Hide resolved
packages/react/src/ErrorBoundary.ko.mdx Outdated Show resolved Hide resolved
packages/react/src/ErrorBoundary.ko.mdx Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – visualization October 14, 2023 13:11 Inactive
@vercel vercel bot temporarily deployed to Preview – visualization October 14, 2023 16:36 Inactive
@manudeli
Copy link
Member Author

I think this hook is very convenient, but how about provide props type guide like this?

import { ErrorBoundaryFallbackProps } from '@suspensive/react'

const Nested = ({ reset, error }: ErrorBoundaryFallbackProps) => {
  return (
    <>
      <button onClick={reset}>reset</button>
      {error.message}
    </>
  )
}

This is exactly same, but can't avoid props drilling.
So, my suggestion is, before provide example that can avoid props drilling case, first show simple example.


And How about add Errorboundary component's visualization page and link it?

hundred words are worth a glance. In korea 백문이불여일견

@minsoo-web could you add suggestion code or Pull Request to this branch please? I want to know what your intend exactly

@minsoo-web
Copy link
Member

@minsoo-web could you add suggestion code or Pull Request to this branch please? I want to know what your intend exactly

I made PR for this case #221
Please review this PR ! 🙇‍♂️

Copy link
Contributor

@SEOKKAMONI SEOKKAMONI left a comment

Choose a reason for hiding this comment

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

I referenced to #221, how about this for document commonization?

packages/react/src/ErrorBoundary.en.mdx Outdated Show resolved Hide resolved
packages/react/src/ErrorBoundary.ko.mdx Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview – visualization October 15, 2023 23:01 Inactive
@vercel vercel bot temporarily deployed to Preview – docs October 15, 2023 23:02 Inactive
minsoo-web and others added 2 commits October 16, 2023 08:58
* docs(errorboundary): change & add example case for ErrorBoundary

* docs(react/errorboundary): change & add example case to ErrorBoundary (en)

* docs(react): suggestion to advise/useErrorBoundaryProps (#227)

* Update ErrorBoundary.ko.mdx

Co-authored-by: Jonghyeon Ko <[email protected]>

* Update ErrorBoundary.en.mdx

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: Jonghyeon Ko <[email protected]>
@vercel vercel bot temporarily deployed to Preview – visualization October 16, 2023 00:01 Inactive
Copy link
Member

@minsoo-web minsoo-web left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@vercel vercel bot temporarily deployed to Preview – docs-legacy October 16, 2023 17:32 Inactive
@vercel vercel bot temporarily deployed to Preview – visualization October 16, 2023 17:34 Inactive
@vercel vercel bot temporarily deployed to Preview – docs October 16, 2023 17:34 Inactive
Comment on lines +60 to +73
import type { ErrorBoundaryFallbackProps } from '@suspensive/react'

const ErrorBoundaryFallback = ({ reset, error }: ErrorBoundaryFallbackProps) => (
<>
<button onClick={reset}>reset</button>
{error.message}
</>
)

const Example = () => (
<ErrorBoundary fallback={ErrorBoundaryFallback}>
<ErrorAfter4s />
</ErrorBoundary>
)
Copy link
Member

Choose a reason for hiding this comment

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

If it's for the purpose of providing typing

Wouldn't it be better for the developer experience to define not only the type of props but also the return?

Suggested change
import type { ErrorBoundaryFallbackProps } from '@suspensive/react'
const ErrorBoundaryFallback = ({ reset, error }: ErrorBoundaryFallbackProps) => (
<>
<button onClick={reset}>reset</button>
{error.message}
</>
)
const Example = () => (
<ErrorBoundary fallback={ErrorBoundaryFallback}>
<ErrorAfter4s />
</ErrorBoundary>
)
import type { ErrorBoundaryFallbackType } from '@suspensive/react'
const ErrorBoundaryFallback: ErrorBoundaryFallbackType = ({ reset, error }) => (
<> // The type of return value is also certain
<button onClick={reset}>reset</button>
{error.message}
</>
)
const Example = () => (
<ErrorBoundary fallback={ErrorBoundaryFallback}>
<ErrorAfter4s />
</ErrorBoundary>
)

I inspried by nextjs GetServerSideProps

Copy link
Member Author

Choose a reason for hiding this comment

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

Wouldn't it be better for the developer experience to define not only the type of props but also the return?

I think that's a good idea too. However, even if we do not provide the return type, TypeScript will create a type error if it does not match the fallback type of ErrorBoundary.

I think we need to consider whether providing a return type is right choice. Because the return type may be different from the actual thing returned (this video explain it well). If it is as essential as ErrorBoundaryFallbackProps, it would be a good idea to add it as a minor version feature.

Let's consider it after merging this

@vercel vercel bot temporarily deployed to Preview – docs October 18, 2023 09:38 Inactive
@vercel vercel bot temporarily deployed to Preview – visualization October 18, 2023 09:39 Inactive
@vercel vercel bot temporarily deployed to Preview – docs-legacy October 18, 2023 09:41 Inactive
@manudeli manudeli merged commit a711c19 into main Oct 18, 2023
13 checks passed
@manudeli manudeli deleted the react/fix/useErrorBoundary-to-stable branch October 18, 2023 09:42
manudeli added a commit that referenced this pull request Aug 3, 2024
* feat(react): add useErrorBoundaryFallbackProps

* chore: update

* Create two-weeks-serve.md

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* docs: update

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* Update packages/react/src/ErrorBoundary.en.mdx

Co-authored-by: 김석진 <[email protected]>

* advise(react/errorboundary): add & change example case (#221)

* docs(errorboundary): change & add example case for ErrorBoundary

* docs(react/errorboundary): change & add example case to ErrorBoundary (en)

* docs(react): suggestion to advise/useErrorBoundaryProps (#227)

* Update ErrorBoundary.ko.mdx

Co-authored-by: Jonghyeon Ko <[email protected]>

* Update ErrorBoundary.en.mdx

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: 김석진 <[email protected]>
Co-authored-by: 김민수 <[email protected]>
manudeli added a commit that referenced this pull request Aug 3, 2024
* feat(react): add useErrorBoundaryFallbackProps

* chore: update

* Create two-weeks-serve.md

* Update packages/react/src/ErrorBoundary.ko.mdx


* Update packages/react/src/ErrorBoundary.ko.mdx


* Update packages/react/src/ErrorBoundary.ko.mdx


* docs: update

* Update packages/react/src/ErrorBoundary.ko.mdx


* Update packages/react/src/ErrorBoundary.en.mdx


* advise(react/errorboundary): add & change example case (#221)

* docs(errorboundary): change & add example case for ErrorBoundary

* docs(react/errorboundary): change & add example case to ErrorBoundary (en)

* docs(react): suggestion to advise/useErrorBoundaryProps (#227)

* Update ErrorBoundary.ko.mdx


* Update ErrorBoundary.en.mdx


---------


---------
manudeli added a commit that referenced this pull request Aug 3, 2024
* feat(react): add useErrorBoundaryFallbackProps

* chore: update

* Create two-weeks-serve.md

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* docs: update

* Update packages/react/src/ErrorBoundary.ko.mdx

Co-authored-by: 김석진 <[email protected]>

* Update packages/react/src/ErrorBoundary.en.mdx

Co-authored-by: 김석진 <[email protected]>

* advise(react/errorboundary): add & change example case (#221)

* docs(errorboundary): change & add example case for ErrorBoundary

* docs(react/errorboundary): change & add example case to ErrorBoundary (en)

* docs(react): suggestion to advise/useErrorBoundaryProps (#227)

* Update ErrorBoundary.ko.mdx

Co-authored-by: Jonghyeon Ko <[email protected]>

* Update ErrorBoundary.en.mdx

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: Jonghyeon Ko <[email protected]>

---------

Co-authored-by: 김석진 <[email protected]>
Co-authored-by: 김민수 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants