Skip to content
Merged
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
20 changes: 11 additions & 9 deletions packages/react-on-rails-pro/src/RSCProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
* https://github.com/shakacode/react_on_rails/blob/master/REACT-ON-RAILS-PRO-LICENSE.md
*/

import * as React from 'react';
'use client';

import React, { createContext, useContext, type ReactNode } from 'react';
import type { ClientGetReactServerComponentProps } from './getReactServerComponent.client.ts';
import { createRSCPayloadKey } from './utils.ts';

type RSCContextType = {
getComponent: (componentName: string, componentProps: unknown) => Promise<React.ReactNode>;
getComponent: (componentName: string, componentProps: unknown) => Promise<ReactNode>;

refetchComponent: (componentName: string, componentProps: unknown) => Promise<React.ReactNode>;
refetchComponent: (componentName: string, componentProps: unknown) => Promise<ReactNode>;
};

const RSCContext = React.createContext<RSCContextType | undefined>(undefined);
const RSCContext = createContext<RSCContextType | undefined>(undefined);

/**
* Creates a provider context for React Server Components.
Expand All @@ -46,9 +48,9 @@ const RSCContext = React.createContext<RSCContextType | undefined>(undefined);
export const createRSCProvider = ({
getServerComponent,
}: {
getServerComponent: (props: ClientGetReactServerComponentProps) => Promise<React.ReactNode>;
getServerComponent: (props: ClientGetReactServerComponentProps) => Promise<ReactNode>;
}) => {
const fetchRSCPromises: Record<string, Promise<React.ReactNode>> = {};
const fetchRSCPromises: Record<string, Promise<ReactNode>> = {};

const getComponent = (componentName: string, componentProps: unknown) => {
const key = createRSCPayloadKey(componentName, componentProps);
Expand All @@ -74,7 +76,7 @@ export const createRSCProvider = ({

const contextValue = { getComponent, refetchComponent };

return ({ children }: { children: React.ReactNode }) => {
return ({ children }: { children: ReactNode }) => {
return <RSCContext.Provider value={contextValue}>{children}</RSCContext.Provider>;
};
};
Expand All @@ -95,11 +97,11 @@ export const createRSCProvider = ({
* @example
* ```tsx
* const { getComponent } = useRSC();
* const serverComponent = React.use(getComponent('MyServerComponent', props));
* const serverComponent = use(getComponent('MyServerComponent', props));
* ```
*/
export const useRSC = () => {
const context = React.useContext(RSCContext);
const context = useContext(RSCContext);
if (!context) {
throw new Error('useRSC must be used within a RSCProvider');
}
Expand Down
16 changes: 8 additions & 8 deletions packages/react-on-rails-pro/src/RSCRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

'use client';

import * as React from 'react';
import React, { Component, use, type ReactNode } from 'react';
import { useRSC } from './RSCProvider.tsx';
import { ServerComponentFetchError } from './ServerComponentFetchError.ts';

/**
* Error boundary component for RSCRoute that adds server component name and props to the error
* So, the parent ErrorBoundary can refetch the server component
*/
class RSCRouteErrorBoundary extends React.Component<
{ children: React.ReactNode; componentName: string; componentProps: unknown },
class RSCRouteErrorBoundary extends Component<
{ children: ReactNode; componentName: string; componentProps: unknown },
{ error: Error | null }
> {
constructor(props: { children: React.ReactNode; componentName: string; componentProps: unknown }) {
constructor(props: { children: ReactNode; componentName: string; componentProps: unknown }) {
super(props);
this.state = { error: null };
}
Expand Down Expand Up @@ -75,9 +75,9 @@ export type RSCRouteProps = {
componentProps: unknown;
};

const PromiseWrapper = ({ promise }: { promise: Promise<React.ReactNode> }) => {
// React.use is available in React 18.3+
const promiseResult = React.use(promise);
const PromiseWrapper = ({ promise }: { promise: Promise<ReactNode> }) => {
// use is available in React 18.3+
const promiseResult = use(promise);

// In case that an error happened during the rendering of the RSC payload before the rendering of the component itself starts
// RSC bundle will return an error object serialized inside the RSC payload
Expand All @@ -88,7 +88,7 @@ const PromiseWrapper = ({ promise }: { promise: Promise<React.ReactNode> }) => {
return promiseResult;
};

const RSCRoute = ({ componentName, componentProps }: RSCRouteProps): React.ReactNode => {
const RSCRoute = ({ componentName, componentProps }: RSCRouteProps): ReactNode => {
const { getComponent } = useRSC();
const componentPromise = getComponent(componentName, componentProps);
return (
Expand Down