Code Monkey home page Code Monkey logo

bs-react-ssr-prepass's Introduction

bs-react-ssr-prepass

BuckleScript bindings for react-ssr-prepass. This allows you to use React Suspense for data fetching on the server-side in ReasonML.

API

These bindings expose a single function, ssrPrepass, that allows you to walk your React tree and look for thrown Promises to trigger Suspense. The argument passed to this function is a polymorphic variant with two possible constructors.

[
  | `PrepassNode(React.element)
  | `PrepassNodeVisitor(React.element, visitor)
]

If you just want to have react-ssr-prepass walk your React tree (or a part of it) and suspend on any thrown Promises, just use the first constructor.

let prepass = ReactSSRPrepass.ssrPrepass(`PrepassNode(<App />));

prepass
|> Js.Promise.then_(() => {
     Js.log("Prepass has finished. Server render the app!");
     Js.Promise.resolve(() => ());
   });

You can also pass a custom visitor function to react-ssr-prepass that will visit each React element in your application and allow you to execute custom data fetching logic based on the element.

let prepass = ReactSSRPrepass.ssrPrepass(`PrepassNodeVisitor(<App />, element => {
  /* Inspect the element as you like to call custom data fetching logic. */
}));

prepass
|> Js.Promise.then_(() => {
     Js.log("Prepass has finished. Server render the app!");
     Js.Promise.resolve(() => ());
   });

Because ReasonReact doesn't expose any utilities for you to introspect React elements themselves (i.e. examine their type, key, or props directly), the ReactSSRPrepass module exposes a few useful functions to deal with this. The first is toElementJS. This function takes in a React.element and returns a [@bs.deriving abstract] object representing the raw React element. You can then use the accessors provided by the [@bs.deriving abstract] to access those fields. An example will help make this clear:

let _ =
  ReactSSRPrepass.ssrPrepass(
    `PrepassNodeVisitor((
      <App />,
      element => {
        /* Get the type property off the element. */
        let elementType =
          ReactSSRPrepass.toElementJS(element) |> ReactSSRPrepass.type_Get;

        /* Get the type property off an arbitrary Component. */
        let componentType =
          Component.make(Js.Obj.empty())
          |> ReactSSRPrepass.toElementJS
          |> ReactSSRPrepass.type_Get;

        /* Check if the types match. */
        if (elementType === componentType) {
          /* Execute specific data fetching logic for this element. */
          Js.log("Execute data fetching logic!");
        };

        /* Return undefined. You can also return a promise from this function. */
        Js.Nullable.undefined;
      },
    )),
  );

Note that toElementJS takes advantage of BuckleScript's %identity trick. It's a bandaid solution as long as we can't get fully type-safe access to the internals of ReasonReact.element.

Maintenance Status

Archived: This project is no longer maintained by Formidable. We are no longer responding to issues or pull requests unless they relate to security concerns. We encourage interested developers to fork this project and make it their own!

bs-react-ssr-prepass's People

Contributors

jpdriver avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  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.