Skip to content

Commit bbc5e96

Browse files
committed
Add source
1 parent 467d5ab commit bbc5e96

14 files changed

+7412
-1
lines changed

.eslintrc.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"parserOptions": {
4+
"ecmaFeatures": {
5+
"jsx": true
6+
},
7+
"project": "./tsconfig.json",
8+
"sourceType": "module"
9+
},
10+
"plugins": ["@typescript-eslint", "import", "react"],
11+
"env": {
12+
"browser": true,
13+
"commonjs": true,
14+
"es2017": true,
15+
"node": true,
16+
"jest": true
17+
},
18+
"settings": {
19+
"react": {
20+
"version": "detect"
21+
}
22+
},
23+
"extends": [
24+
"eslint:recommended",
25+
"plugin:react/recommended",
26+
"plugin:@typescript-eslint/eslint-recommended",
27+
"plugin:@typescript-eslint/recommended",
28+
"prettier",
29+
"prettier/@typescript-eslint"
30+
],
31+
"rules": {
32+
"@typescript-eslint/explicit-function-return-type": [
33+
"warn",
34+
{
35+
"allowExpressions": true
36+
}
37+
],
38+
"@typescript-eslint/no-explicit-any": "off",
39+
"@typescript-eslint/no-floating-promises": "warn",
40+
"@typescript-eslint/no-use-before-define": [
41+
"warn",
42+
{
43+
"functions": false,
44+
"classes": false,
45+
"variables": false,
46+
"typedefs": false
47+
}
48+
],
49+
"@typescript-eslint/no-unused-vars": [
50+
"warn",
51+
{
52+
"args": "none",
53+
"ignoreRestSiblings": true
54+
}
55+
],
56+
"import/order": "warn",
57+
"no-console": "warn"
58+
},
59+
"overrides": [
60+
{
61+
"files": ["**/*.test.ts?(x)"],
62+
"rules": {
63+
// Disable rules that are lower value in tests
64+
"@typescript-eslint/explicit-function-return-type": "off",
65+
"@typescript-eslint/no-non-null-assertion": "off"
66+
}
67+
}
68+
]
69+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
lib/

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"tabWidth": 4,
3+
"trailingComma": "es5"
4+
}

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Debug Jest Tests",
9+
"type": "node",
10+
"request": "launch",
11+
"runtimeArgs": [
12+
"--inspect-brk",
13+
"${workspaceRoot}/node_modules/.bin/jest",
14+
"--runInBand",
15+
"--watch"
16+
],
17+
"console": "integratedTerminal",
18+
"internalConsoleOptions": "neverOpen",
19+
"port": 9229
20+
}
21+
]
22+
}

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"eslint.validate": [
3+
"javascript",
4+
"javascriptreact",
5+
{ "language": "typescript", "autoFix": true },
6+
{ "language": "typescriptreact", "autoFix": true }
7+
]
8+
}

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
1-
# react-lazy-with-preload
1+
`react-lazy-with-preload` wraps the `React.lazy()` API and adds the ability to preload the component before it is rendered for the first time.
2+
3+
## Install
4+
5+
```sh
6+
npm install react-lazy-with-preload
7+
```
8+
9+
## Usage
10+
11+
**Before:**
12+
13+
```js
14+
import { lazy, Suspense } from "react";
15+
const OtherComponent = lazy(() => import("./OtherComponent"));
16+
```
17+
18+
**After:**
19+
20+
```js
21+
import { Suspense } from "react";
22+
import lazy from "react-lazy-with-preload";
23+
const OtherComponent = lazy(() => import("./OtherComponent"));
24+
25+
// ...
26+
OtherComponent.preload();
27+
```
28+
29+
For more information about React code-splitting, `React.lazy` and `React.Suspense`, see https://reactjs.org/docs/code-splitting.html.
30+
31+
## Example
32+
33+
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.
34+
35+
For example, if you need to load a component when a button is pressed, you could start preloading the component when the user hovers over the button:
36+
37+
```js
38+
function SomeComponent() {
39+
const { showOtherComponent, setshowOtherComponent } = useState(false);
40+
41+
return (
42+
<div>
43+
<Suspense fallback={<div>Loading...</div>}>
44+
{showOtherComponent && <OtherComponent />}
45+
</Suspense>
46+
<button
47+
onClick={() => setshowOtherComponent(true)}
48+
// This component will be needed soon. Let's preload it!
49+
onMouseOver={() => OtherComponent.preload()}
50+
>
51+
Click me to render OtherComponent
52+
</button>
53+
</div>
54+
);
55+
}
56+
```
57+
58+
## Acknowledgements
59+
60+
Inspired by the preload behavior of [react-loadable](https://github.com/jamiebuilds/react-loadable).

babel.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-react",
5+
"@babel/preset-typescript",
6+
],
7+
};

jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
// Automatically clear mock calls and instances between every test
3+
clearMocks: true,
4+
roots: ["<rootDir>/src/"],
5+
watchPlugins: [
6+
"jest-watch-typeahead/filename",
7+
"jest-watch-typeahead/testname",
8+
],
9+
};

0 commit comments

Comments
 (0)