Git Product home page Git Product logo

next-extra's Introduction

next-extra

Enhance your Next.js projects with additional methods not found in the core package.

Installation

npm install next-extra

Usage

next-extra/action

API
function createAction(fn: Function): ActionFunc;
function actionError(code: string, message: string): never;
function cookies(): ResponseCookies;
function clientIP(): string;
Example
// -- 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

This module provides utilities for passing serializable data from the server layout to client page components in the Next.js App Router. It is particularly useful for sharing context-specific data across your application without the need to re-fetch data, thereby saving computing resources and improving performance.

API
function PageContext<T = any>(props: PageContextProps<T>): JSX.Element;
function usePageContext<T = any>(opts: UsePageContextOptions): T;
Example
// -- 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';

interface Context {
  ts: number
  quote: string
}

export default function Page() {
  const { ts, quote } = usePageContext<Context>();

  console.log('Timestamp: ', ts);

  return <h1>{quote}</h1>;
}

next-extra/pathname

Access pathname and searchParams of the incoming request for server-side components in the App Router.

API
function invokeUrl(): URL;
function pathname(): string;
function searchParams(): ReadonlyURLSearchParams;
Example
import { invokeUrl, pathname, searchParams } from 'next-extra/pathname';

export default async function Layout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  const url = invokeUrl(); // instance of URL; http://localhost:3000/hello?name=John
  const path = pathname(); // /hello
  const params = searchParams(); // ReadonlyURLSearchParams { 'name' => 'John' }

  return children;
}

Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.

Thanks again for your support, it is much appreciated! 🙏

License

MIT © Shahrad Elahi

next-extra's People

Contributors

shahradelahi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.