next-request-block
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.1 • Public • Published

    next-request-block

    React library for integrating react-request-block into the server-side rendering of your Next.js app.

    Install

    Via npm

    npm install --save next-request-block

    Via Yarn

    yarn add next-request-block

    How to use

    withRequestBlock (HOC)

    withRequestBlock makes it super simple to setup react-request-block within your Next.js app. Using next-request-block makes sure that all your RequestBlock instances are fetched and rendered server-side, allowing you to truly take advantage of Next.js, while making it easy to compose requests/data into your Next + React apps.

    Options

    Option Default Description
    origin http(s)://[current host] (server-side) or '' (client-side) Origin to use for RequestBlock requests. Default to current protocol/host of app.
    options null Default options that can be set for all RequestBlock instances. Although, can be overriden or ignore per RequestBlock.

    Example w/o options

    import App, { Container } from 'next/app';
    import React from 'react';
    import { withRequestBlock } from 'next-request-block';
     
    class MyApp extends App {
      static async getInitialProps({ Component, ctx, router }) {
        let pageProps = {};
     
        if (Component.getInitialProps) {
          pageProps = await Component.getInitialProps({ ctx });
        }
     
        return { pageProps };
      }
     
      render() {
        const {
          Component,
          pageProps,
          store,
        } = this.props;
     
        return (
          <Container>
            <Component {...pageProps} />
          </Container>
        );
      }
    }
     
    export default withRequestBlock()(MyApp);

    Example w/ options

    import App, { Container } from 'next/app';
    import React from 'react';
    import { withRequestBlock } from 'next-request-block';
     
    class MyApp extends App {
      static async getInitialProps({ Component, ctx, router }) {
        let pageProps = {};
     
        if (Component.getInitialProps) {
          pageProps = await Component.getInitialProps({ ctx });
        }
     
        return { pageProps };
      }
     
      render() {
        const {
          Component,
          pageProps,
          store,
        } = this.props;
     
        return (
          <Container>
            <Component {...pageProps} />
          </Container>
        );
      }
    }
     
    export default withRequestBlock({
      origin: 'https://api.custom.origin',
      options: {{ headers: { 'X-CSRF-TOKEN': 'adaalahlahfih7fhb7kaflhdalkf' }}},
    })(MyApp);

    License

    MIT © Ryan Hefner

    Install

    npm i next-request-block

    DownloadsWeekly Downloads

    1

    Version

    0.2.1

    License

    MIT

    Unpacked Size

    27.5 kB

    Total Files

    18

    Last publish

    Collaborators

    • avatar