Skip to content

[Feature]: Suspense.with with fallback callback prop #1799

@ojj1123

Description

@ojj1123

Package Scope

@suspensive/react

Description

Hi, I'm using suspensive at my company.

I'd like to suggest supporting for Suspense.with with fallback callback prop (called render prop). I think this is useful when we want to adjust the fallback ui depending on passing props.

const MySuspendedComp = (props) => {
  const { data } = useSuspenseQuery(...);

  return <>...</>;
};


export default Suspense.with({ fallback: (props) => <FallbackWithProps {...props} /> }, MySuspendedComp);

// of course, also possible to pass component as `ReactNode`.
// export default Suspense.with({ fallback: <Fallback /> }, MySuspendedComp);

Possible Solution

We can try to get fallback as (props) => ReactNode | ReactNode like below:
ref: source code

  with: <TProps extends ComponentProps<ComponentType> = Record<string, never>>(
    suspenseProps: PropsWithoutChildren<SuspenseProps> = {},
    Component: ComponentType<TProps>
  ) =>
    Object.assign(
      (props: TProps) => {
        const { fallbackProp, ...rest } = suspenseProps;

        const fallback =
          typeof fallbackProp === 'function'
            ? fallbackProp(props)
            : fallbackProp;

        return (
          <Suspense {...rest} fallback={fallback}>
            <Component {...props} />
          </Suspense>
        );
      },
      {
        displayName: `Suspense.with(${
          Component.displayName || Component.name || 'Component'
        })`,
      }
    ),

etc.

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions