next extra
v0.6.1

Next-Extra是一个实用程序软件包,它允许您使用Core软件包中未找到的其他方法来增强下一个项目。
next-extra/actionnext-extra/contextnext-extra/pathnamenpm install next-extranext-extra/action function createAction ( fn : Function ) : ActionFunc ;
function actionError ( code : string , message : string ) : never ;
function cookies ( ) : ResponseCookies ;
function clientIP ( ) : Promise < string | null > ; // -- actions.ts
'use server' ;
import { actionError , createAction } from 'next-extra/action' ;
export const hello = createAction ( async ( name : string ) => {
if ( ! name ) {
actionError ( 'NAME_REQUIRED' , 'Name is required' ) ;
}
return `Hello, ${ name } !` ;
} ) ; // -- page.tsx
import { hello } from './actions' ;
export default async function Page ( ) {
const { data , error } = await hello ( 'John' ) ;
if ( error ) {
return < h1 > ERROR : { error . message } < / h1 > ;
}
return < h1 > { data } < / h1>;
}next-extra/context该模块提供了用于将可序列化数据从服务器布局传递到Next.js应用程序路由器中客户端页面组件的实用程序。它对于在您的应用程序上共享特定于上下文的数据特别有用,而无需重新填写数据,从而节省计算资源并提高性能。
function PageContext < T > ( props : PageContextProps < T > ) : JSX . Element ;
function usePageContext < T > ( ) : Readonly < T > ;
function useServerInsertedContext < T > ( ) : Readonly < T | undefined > ; // -- layout.tsx
import { PageContext } from 'next-extra/context' ;
export default async function RootLayout ( { children } : { children : React . ReactNode } ) {
return < PageContext data = { { ts : Date . now ( ) } } > { children } < / PageContext>;
} // -- quotes/layout.tsx
import { PageContext } from 'next-extra/context' ;
export default async function Layout ( { children } : { children : React . ReactNode } ) {
return < PageContext data = { { quote : 'Guillermo Rauch is a handsome dude!' } } > { children } < / PageContext>;
} // -- quotes/page.tsx
'use client' ;
import { useServerInsertedContext , usePageContext } from 'next-extra/context' ;
interface Context {
message : string ;
}
interface InsertedContext extends Context {
ts : number ;
}
export default function Page ( ) {
const insertedCtx = useServerInsertedContext < InsertedContext > ( ) ;
console . log ( insertedCtx ) ; // undefined in server or Object { ts: ..., message: "..." }
const ctx = usePageContext < Context > ( ) ;
console . log ( ctx ) ; // Object { message: "..." }
return < h3 > Message : { ctx . message } < / h3 > ;
}next-extra/pathname访问应用程序组件中服务器端组件的传入请求的访问pathname和searchParams 。
function pathname ( ) : Promise < string > ;
function searchParams ( ) : Promise < ReadonlyURLSearchParams > ; import { pathname , searchParams } from 'next-extra/pathname' ;
export default async function Layout ( {
children ,
} : Readonly < { children : React . ReactNode } > ) {
// Assuming a request to "/hello?name=John"
const route = await pathname ( ) ; // /hello
const params = await searchParams ( ) ; // ReadonlyURLSearchParams { 'name' => 'John' }
return children ;
} 想贡献吗?惊人的!表示您的支持是饰演该项目,或在Github上提出问题。
再次感谢您的支持,非常感谢!
麻省理工学院©Shahrad Elahi