Skip to content

Commit

Permalink
fix(react): add isDevelopment to remove repetitive implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli committed Jan 27, 2023
1 parent e852516 commit 31be0fb
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/react/src/AsyncBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ComponentProps, ComponentRef, forwardRef } from 'react'
import { ErrorBoundary } from './ErrorBoundary'
import { Suspense } from './Suspense'
import { isDevelopment } from './utils'

type SuspenseProps = ComponentProps<typeof Suspense>
type ErrorBoundaryProps = ComponentProps<typeof ErrorBoundary>
Expand All @@ -17,7 +18,7 @@ const BaseAsyncBoundary = forwardRef<ComponentRef<typeof ErrorBoundary>, Props>(
</ErrorBoundary>
)
)
if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
BaseAsyncBoundary.displayName = 'AsyncBoundary'
}
const CSROnlyAsyncBoundary = forwardRef<ComponentRef<typeof ErrorBoundary>, Props>(
Expand All @@ -27,7 +28,7 @@ const CSROnlyAsyncBoundary = forwardRef<ComponentRef<typeof ErrorBoundary>, Prop
</ErrorBoundary>
)
)
if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
CSROnlyAsyncBoundary.displayName = 'AsyncBoundary.CSROnly'
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
useRef,
} from 'react'
import { ErrorBoundaryGroupContext } from './ErrorBoundaryGroup'
import { isDifferentArray } from './utils'
import { isDevelopment, isDifferentArray } from './utils'

type Props = PropsWithRef<
PropsWithChildren<{
Expand Down Expand Up @@ -121,6 +121,6 @@ export const ErrorBoundary = forwardRef<{ reset(): void }, ComponentPropsWithout
return <BaseErrorBoundary {...props} resetKeys={resetKeys} ref={ref} />
}
)
if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
ErrorBoundary.displayName = 'ErrorBoundary'
}
5 changes: 3 additions & 2 deletions packages/react/src/ErrorBoundaryGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
useMemo,
} from 'react'
import { useIsMounted, useKey } from './hooks'
import { isDevelopment } from './utils'

export const ErrorBoundaryGroupContext = createContext({ resetKey: 0, reset: () => {} })
if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
ErrorBoundaryGroupContext.displayName = 'ErrorBoundaryGroupContext'
}

Expand Down Expand Up @@ -85,7 +86,7 @@ export const withErrorBoundaryGroup = <P extends Record<string, unknown> = Recor
</ErrorBoundaryGroup>
)

if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
const name = Component.displayName || Component.name || 'Component'
Wrapped.displayName = `withErrorBoundaryGroup(${name})`
}
Expand Down
9 changes: 5 additions & 4 deletions packages/react/src/Suspense.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Suspense as BaseSuspense, SuspenseProps, ComponentProps, ComponentType } from 'react'
import { useIsMounted } from './hooks'
import { isDevelopment } from './utils'

type Props = SuspenseProps
const DefaultSuspense = (props: Props) => <BaseSuspense {...props} />
if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
DefaultSuspense.displayName = 'Suspense'
}
const CSROnlySuspense = (props: Props) => (useIsMounted() ? <BaseSuspense {...props} /> : <>{props.fallback}</>)
if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
CSROnlySuspense.displayName = 'Suspense.CSROnly'
}

Expand All @@ -34,7 +35,7 @@ export function withSuspense<Props extends Record<string, unknown> = Record<stri
</Suspense>
)

if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
const name = Component.displayName || Component.name || 'Component'
Wrapped.displayName = `withSuspense(${name})`
}
Expand All @@ -52,7 +53,7 @@ withSuspense.CSROnly = function withSuspenseCSROnly<Props extends Record<string,
</Suspense.CSROnly>
)

if (process.env.NODE_ENV !== 'production') {
if (isDevelopment) {
const name = Component.displayName || Component.name || 'Component'
Wrapped.displayName = `withSuspense.CSROnly(${name})`
}
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as isDifferentArray } from './isDifferentArray'
export { default as isDevelopment } from './isDevelopment'
3 changes: 3 additions & 0 deletions packages/react/src/utils/isDevelopment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const isDevelopment = process.env.NODE_ENV !== 'production'

export default isDevelopment

0 comments on commit 31be0fb

Please sign in to comment.