Skip to content

PPX proposal: React.lazy #498

@bloodyowl

Description

@bloodyowl

it's currently pretty hard to get a type-safe dynamic import using BuckleScript. it's even harder to use React.lazy.

how would you feel about having a PPX:

module ReactLazy = {
  [@bs.module "react"]
  external make:
    (unit => Js.Promise.t({. "default": React.component('a)})) =>
    React.component('a) =
    "lazy";
};

[@react.lazy]
module MyComponentLazy = MyComponent;

// that would expand to something like:
module MyComponentLazy: MyComponentType = {
  module type MyComponentType = (module type of MyComponent);
  [@bs.val] external myComponent: (module MyComponentType) = "undefined";
  include (val myComponent);
  [@bs.val]
  external import:
    ([@bs.as "MODULE_RELATIVE_PATH"] _, unit) => Js.Promise.t(module MyComponentType) =
    "import";
  let make =
    ReactLazy.make(() =>
      import()
      |> Js.Promise.then_((module MyComponent: MyComponentType) =>
           Js.Promise.resolve({"default": MyComponent.make})
         )
    );
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCppxissues related to the reason-react ppx

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions