From 30a2daad3e3520ce8805b18eb05bf18bd7307513 Mon Sep 17 00:00:00 2001 From: Ian Schmitz Date: Mon, 25 Jul 2022 10:27:28 -0700 Subject: [PATCH] Add named export `lazyWithPreload` --- README.md | 6 +++--- package-lock.json | 4 ++-- package.json | 2 +- src/__tests__/index.test.tsx | 6 +++++- src/index.ts | 4 +++- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8946791..9ad7977 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package-lock.json b/package-lock.json index f89426f..b258cee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "react-lazy-with-preload", - "version": "2.1.0", + "version": "2.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "react-lazy-with-preload", - "version": "2.1.0", + "version": "2.2.0", "license": "MIT", "devDependencies": { "@babel/core": "^7.10.2", diff --git a/package.json b/package.json index 065820a..fa142b0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/index.test.tsx b/src/__tests__/index.test.tsx index 6bf3ad6..da599b6 100644 --- a/src/__tests__/index.test.tsx +++ b/src/__tests__/index.test.tsx @@ -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< @@ -154,4 +154,8 @@ describe("lazy", () => { expect(preloadedComponent).toBe(OriginalComponent); }); + + it("exports named export as well", () => { + expect(lazy).toBe(namedExport); + }); }); diff --git a/src/index.ts b/src/index.ts index 4efdb52..d88b1dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ export type PreloadableComponent> = T & { preload: () => Promise; }; -export default function lazyWithPreload>( +export function lazyWithPreload>( factory: () => Promise<{ default: T }> ): PreloadableComponent { const LazyComponent = lazy(factory); @@ -31,3 +31,5 @@ export default function lazyWithPreload>( return Component; } + +export default lazyWithPreload;