最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct Next.js 15 server action type in TypeScript - how to definetype component's server action property?

programmeradmin3浏览0评论

How to correctly type server action functions passed in to a child components as properties?

Example server action definition:

export const addProduct = async (product: Product): Promise<{ success: boolean } | null> => {
  // do something
  return {
    success: true
  };
}

Example page.tsx:

import { addProduct } from '@/actions/products-actions';

const ProductsAddPage = async () => {
  return <>
    <h1>Add Post</h1>
    <section>
      <ProductForm
        action={addProduct}
        actionType='add'
     />
    </section>
  </>;
}

export default ProductsAddPage;

And here is ProductForm definition:

'use client';

interface ProductFormProps {
  action: any; // this is where I have no clue what to do? 
  actionType: string;
}

const ProductForm = (
  {
    action,
    actionType
  }: ProductFormProps
) => {

  return <>
    {/* show something */}
  </>;

}

so I understand there are 'generics' where we can make the interface ProductFormProps property action instead of any, a generic type so I can pass any type of server action function to it so it's not just a single type definition?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论