Skip to content

Add React.lazy #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/React.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/React.res
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ module Experimental = {
}
}

type dynamicallyImportedModule<'a> = {default: component<'a>}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much context on this so will ask very basic questions.
Do we know the component will have default export? Is this a property that react lazy relies on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the shape that React.lazy expects.


@module("react")
external lazy_: (unit => promise<dynamicallyImportedModule<'a>>) => component<'a> = "lazy"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the underscore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy is a reserved word unfortunately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right it's on the list.
Perhaps worth opening an issue to explore whether it absolutely needs to be a keyword.
We removed a few keywords successfully recently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy is a nice add-on, but I think it can be moved to the standard library rather than the language.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lazy is a nice add-on, but I think it can be moved to the standard library rather than the language.

If I understand correctly, this is a rescript-react repo, not compiler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue here: rescript-lang/rescript#6241


let lazy_ = load => lazy_(async () => {default: await load()})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, do we need to expose this wrapper function? If not, would user usage be different?

Copy link
Member Author

@cknitt cknitt May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we didn't expose this wrapper function, the user would need to write

let make = React.lazy_(async () => {default: await Js.import(DynamicallyImportedComponent.make)})

instead of

let make = React.lazy_(() => Js.import(DynamicallyImportedComponent.make))


/* HOOKS */

/*
Expand Down