Skip to content

Commit

Permalink
Add named export lazyWithPreload
Browse files Browse the repository at this point in the history
  • Loading branch information
ianschmitz committed Jul 25, 2022
1 parent bf5ace8 commit 30a2daa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const OtherComponent = lazy(() => import("./OtherComponent"));

```js
import { Suspense } from "react";
import lazy from "react-lazy-with-preload";
const OtherComponent = lazy(() => import("./OtherComponent"));
import { lazyWithPreload } from "react-lazy-with-preload";
const OtherComponent = lazyWithPreload(() => import("./OtherComponent"));

// ...
OtherComponent.preload();
```

To preload a component before it is rendered for the first time, the component that is returned from `lazy()` has a `preload` function attached that you can invoke. `preload()` returns a `Promise` that you can wait on if needed. The promise is idempotent, meaning that `preload()` will return the same `Promise` instance if called multiple times.
To preload a component before it is rendered for the first time, the component that is returned from `lazyWithPreload()` has a `preload` function attached that you can invoke. `preload()` returns a `Promise` that you can wait on if needed. The promise is idempotent, meaning that `preload()` will return the same `Promise` instance if called multiple times.

For more information about React code-splitting, `React.lazy` and `React.Suspense`, see https://reactjs.org/docs/code-splitting.html.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-lazy-with-preload",
"version": "2.1.0",
"version": "2.2.0",
"description": "Wraps the React.lazy API with preload functionality",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { act, render, screen, waitFor } from "@testing-library/react";
import lazy from "../index";
import lazy, { lazyWithPreload as namedExport } from "../index";

function getTestComponentModule() {
const TestComponent = React.forwardRef<
Expand Down Expand Up @@ -154,4 +154,8 @@ describe("lazy", () => {

expect(preloadedComponent).toBe(OriginalComponent);
});

it("exports named export as well", () => {
expect(lazy).toBe(namedExport);
});
});
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type PreloadableComponent<T extends ComponentType<any>> = T & {
preload: () => Promise<T>;
};

export default function lazyWithPreload<T extends ComponentType<any>>(
export function lazyWithPreload<T extends ComponentType<any>>(
factory: () => Promise<{ default: T }>
): PreloadableComponent<T> {
const LazyComponent = lazy(factory);
Expand All @@ -31,3 +31,5 @@ export default function lazyWithPreload<T extends ComponentType<any>>(

return Component;
}

export default lazyWithPreload;

0 comments on commit 30a2daa

Please sign in to comment.