feat: add Preact support and example
- Add Preact setup instructions and code examples alongside existing React examples - Maintain existing React functionality while adding Preact support - Update code examples to show both React and Preact imports react hooks come directly from 'react' package, while Preact requires hooks to be imported separately from 'preact/hooks'
This commit is contained in:
parent
952025b996
commit
a8395e7139
@ -277,7 +277,12 @@ This complete component example fetches a test file and displays the contents in
|
||||
a HTML table. When the export button is clicked, a callback will export a file:
|
||||
|
||||
```jsx title="src/SheetJSReactAoO.js"
|
||||
// React imports
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
// Preact imports
|
||||
// import { render } from 'preact';
|
||||
// import { useCallback, useEffect, useState } from 'preact/hooks';
|
||||
|
||||
import { read, utils, writeFileXLSX } from 'xlsx';
|
||||
|
||||
export default function SheetJSReactAoO() {
|
||||
@ -558,6 +563,73 @@ Access the displayed URL (typically `http://localhost:3000`) with a web browser
|
||||
and test the page.
|
||||
|
||||
</TabItem>
|
||||
<TabItem name="preact" value="Preact">
|
||||
:::note Tested Deployments
|
||||
|
||||
This demo was tested in the following environments:
|
||||
|
||||
| Preact | ViteJS | Date |
|
||||
|:----------|:----------|:-----------|
|
||||
| `10.22.1` | `5.3.3` | 2024-12-17 |
|
||||
:::
|
||||
|
||||
1) Create a new site:
|
||||
|
||||
```bash
|
||||
npm init preact sheetjs-preact
|
||||
```
|
||||
|
||||
This will initiate the project creation process. **Follow the on-screen prompts and
|
||||
press Enter to accept the default options.**
|
||||
|
||||
2) Install the SheetJS dependency and start the dev server:
|
||||
|
||||
|
||||
<CodeBlock language="bash">{`\
|
||||
cd sheetjs-preact
|
||||
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
|
||||
npm run dev`}
|
||||
</CodeBlock>
|
||||
|
||||
3) Open a web browser and access the displayed URL (`http://localhost:5173`)
|
||||
|
||||
4) Copy `src/SheetJSReactAoO.js` demo code to `src/index.jsx` and update the imports:
|
||||
|
||||
```jsx
|
||||
// Remove React import and replace with:
|
||||
import { render } from 'preact';
|
||||
import { useCallback, useEffect, useState } from 'preact/hooks';
|
||||
```
|
||||
|
||||
5) Add the following to the bottom of the file:
|
||||
|
||||
```jsx
|
||||
export function App() { return ( <SheetJSReactAoO/> );}
|
||||
|
||||
render(<App />, document.getElementById('app'));
|
||||
```
|
||||
|
||||
The page will refresh and show a table with an Export button. Click the button
|
||||
and the page will attempt to download `SheetJSReactAoO.xlsx`.
|
||||
|
||||
6) Build the site:
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
The generated site will be placed in the `dist` folder.
|
||||
|
||||
7) Start a local web server:
|
||||
|
||||
```bash
|
||||
npx http-server dist
|
||||
```
|
||||
|
||||
Access the displayed URL (typically `http://localhost:8080`) with a web browser
|
||||
and test the page.
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
When the page loads, the app will fetch https://docs.sheetjs.com/pres.xlsx and
|
||||
@ -582,7 +654,12 @@ export, the first `TABLE` child element can be parsed with [`table_to_book`](/do
|
||||
generate a workbook object.
|
||||
|
||||
```jsx title="src/SheetJSReactHTML.js"
|
||||
// React imports
|
||||
import React, { useCallback, useEffect, useRef, useState } from "react";
|
||||
// Preact imports
|
||||
// import { render } from 'preact';
|
||||
// import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
||||
|
||||
import { read, utils, writeFileXLSX } from 'xlsx';
|
||||
|
||||
export default function SheetJSReactHTML() {
|
||||
@ -732,6 +809,71 @@ The generated site will be placed in the `build` folder.
|
||||
npx http-server build
|
||||
```
|
||||
|
||||
Access the displayed URL (typically `http://localhost:8080`) with a web browser
|
||||
and test the page.
|
||||
|
||||
</TabItem>
|
||||
<TabItem name="preact" value="Preact">
|
||||
|
||||
:::note Tested Deployments
|
||||
This demo was tested in the following environments:
|
||||
|
||||
| Preact | ViteJS | Date |
|
||||
|:----------|:--------|:-----------|
|
||||
| `10.22.1` | `5.3.3` | 2024-12-17 |
|
||||
:::
|
||||
|
||||
```bash
|
||||
npm init preact sheetjs-preact
|
||||
```
|
||||
|
||||
This will initiate the project creation process. **Follow the on-screen prompts and
|
||||
press Enter to accept the default options.**
|
||||
|
||||
2) Install the SheetJS dependency and start the dev server:
|
||||
|
||||
|
||||
<CodeBlock language="bash">{`\
|
||||
cd sheetjs-preact
|
||||
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
|
||||
npm run dev`}
|
||||
</CodeBlock>
|
||||
|
||||
3) Open a web browser and access the displayed URL (`http://localhost:5173`)
|
||||
|
||||
4) Copy `src/SheetJSReactHTML.js` demo code to `src/index.jsx` and update the imports:
|
||||
|
||||
```jsx
|
||||
// Remove React import and replace with:
|
||||
import { render } from 'preact';
|
||||
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
||||
```
|
||||
|
||||
5) Add the following to the bottom of the file:
|
||||
|
||||
```jsx
|
||||
export function App() { return ( <SheetJSReactHTML/> );}
|
||||
|
||||
render(<App />, document.getElementById('app'));
|
||||
```
|
||||
|
||||
The page will refresh and show a table with an Export button. Click the button
|
||||
and the page will attempt to download `SheetJSReactHTML.xlsx`.
|
||||
|
||||
6) Build the site:
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
The generated site will be placed in the `dist` folder.
|
||||
|
||||
7) Start a local web server:
|
||||
|
||||
```bash
|
||||
npx http-server dist
|
||||
```
|
||||
|
||||
Access the displayed URL (typically `http://localhost:8080`) with a web browser
|
||||
and test the page.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user