#How do we type annotate props for a server component in NextJS?
1 messages · Page 1 of 1 (latest)
In Next.js, when passing data between a Server and Client Component through props, the data is serialized (converted to a string) for use in the browser. However, it does not need a special type. It’s typed the same as passing any other props between components.
For example, if you are passing a Server Action to a Client Component as a prop, you might do something like this:
import { ExampleClientComponent } from './components/example-client-component.js'
function ExampleServerComponent({ id }) {
async function updateItem(data) {
'use server'
modifyItem({ id, data })
}
return <ExampleClientComponent updateItem={updateItem} />
}
In this example, updateItem is a prop being passed to ExampleClientComponent. The type of updateItem would be the same as the type of the function updateItem in ExampleServerComponent.
If you are using TypeScript, ensure you are using TypeScript 5.1.3 or higher and @types/react 18.2.8 or higher to avoid any type errors when using async Server Components.