Interact is a Rsc server.
By default, all React components runs on the server and are server components.
Component Types
Server Component
By default, all React Components:
- runs on the server and can call server module. For instance, if it runs on Node, you can import and call all Nodes module
- but they don't have access to the Browser (no window, click, ...)
If a component needs interactivity or the browser environment, you need to make it a client component with the use client directive.
Client Component
The React use client directive when found at the top of a script makes it a Client component (ie interactive component)
A React Client component will:
- be shipped to the browser
- but it will also run on the server in SSR mode
Example: If you use the window global, you may:
- move it into a useEffect (client-only)
- or guard it
Support
Only plain objects error
You may get this error
This error is mostly due because a component or props in the React tree has the value null.
Why? Here are the possible reasons:
- the component does not exist in the imported module. Example, the imported NavBar value below may be null if the component is not exported by default.
- your layout component is a client component and it should not. The client component is in the client bundle and will return null on the server.
- you are passing a props with a null value. Example: This partial is passing the whole props but the layout props have also the request context that contains null.
The correct fix is:
Invalid Hook Call Warning in Rsc environment
In React, you may get the injurious invalid hook call warning.
In Rsc, this problem may occur because you might have more than one copy of React due to bad splitting.
Why? The bundler needs to parse every file that have the use client directive in order to split correctly the code between the client and the servers environments (rsc/ssr).
By default, in SSR, all dependencies are externalized and are not processed (except for linked dependencies for HMR). Therefore, the React library dependency needs to be listed in the noExternal properties.
We do it for you by scanning your package.json in search of library with react in their dependencies and peerDependencies properties.
If the incriminated dependency is a React library, you can:
- add react as peerDependencies
- or create your vite configuration and adding the library in the noExternal properties for the server environments (ssr and rsc)