|
1 | 1 | # React Isomorphic ScriptLoader |
2 | 2 |
|
3 | | -React package to load scripts into your isomorphic (server side rendered) webapp. |
| 3 | +A lightweight React component to load scripts into your isomorphic (server-side rendered) web application. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Isomorphic/Universal**: Works on both server (SSR) and client. |
| 8 | +- **Modern Stack**: Built with TypeScript, supports React 18 and React 19. |
| 9 | +- **Zero Dependencies**: Lightweight and efficient. |
| 10 | +- **Typed**: Includes first-class TypeScript definitions. |
4 | 11 |
|
5 | 12 | ## Installation |
6 | | -``` |
| 13 | + |
| 14 | +```bash |
| 15 | +# npm |
| 16 | +npm install react-isomorphic-scriptloader |
| 17 | + |
| 18 | +# yarn |
7 | 19 | yarn add react-isomorphic-scriptloader |
| 20 | + |
| 21 | +# pnpm |
| 22 | +pnpm add react-isomorphic-scriptloader |
8 | 23 | ``` |
9 | 24 |
|
10 | | -## Example Usage |
11 | | - |
12 | | -```js |
13 | | -import React from 'react' |
14 | | -import Loader from 'react-isomorphic-scriptloader' |
15 | | - |
16 | | -export default class MyComponent extends React.Component { |
17 | | - state = { scriptsLoaded: false }; |
18 | | - render() { |
19 | | - return ( |
20 | | - <div> |
21 | | - <Loader src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places" onLoad={() => this.setState({ scriptsLoaded: true })} /> |
22 | | - { this.state.scriptsLoaded && <div>Yayy google maps loaded</div> } |
23 | | - { !this.state.scriptsLoaded && <div>Please wait while google maps is being loaded</div> } |
24 | | - </div> |
25 | | - ) |
26 | | - } |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Functional Component (Hooks) |
| 28 | + |
| 29 | +```tsx |
| 30 | +import React, { useState } from 'react'; |
| 31 | +import ScriptLoader from 'react-isomorphic-scriptloader'; |
| 32 | + |
| 33 | +export default function MyComponent() { |
| 34 | + const [scriptsLoaded, setScriptsLoaded] = useState(false); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div> |
| 38 | + <ScriptLoader |
| 39 | + src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places" |
| 40 | + onLoad={() => setScriptsLoaded(true)} |
| 41 | + /> |
| 42 | + |
| 43 | + {scriptsLoaded ? ( |
| 44 | + <div>Google Maps Loaded!</div> |
| 45 | + ) : ( |
| 46 | + <div>Loading Maps...</div> |
| 47 | + )} |
| 48 | + </div> |
| 49 | + ); |
27 | 50 | } |
28 | 51 | ``` |
29 | 52 |
|
| 53 | +## Props |
| 54 | + |
| 55 | +| Prop | Type | Description | |
| 56 | +|------|------|-------------| |
| 57 | +| `src` | `string` | The URL of the script to load. | |
| 58 | +| `onLoad` | `() => void` | Callback function executed when the script has loaded. | |
| 59 | + |
30 | 60 | ## License |
31 | 61 |
|
32 | | -This package is licensed under the terms of MIT License. See the LICENSE file for further information. |
| 62 | +This package is licensed under the MIT License. |
0 commit comments