Skip to content

Commit 06c696e

Browse files
update to internal commit bd73dd7d
1 parent a29fb24 commit 06c696e

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

getstarted/react.md

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,34 @@ permalink: /indepth/development/react.html
1414

1515
## Preparation
1616

17-
Make sure you have [node](https://nodejs.org/) and [yarn](https://yarnpkg.com/cli/install) installed. `node 14.4.0` and `yarn 1.22.4` are used in the example below.
17+
Make sure you have [node](https://nodejs.org/) installed. `node 20.18.2` is used in the example below.
1818

1919
## Create the Sample Project
2020

21-
### Create a Bootstrapped Raw React Application
21+
### Create a React project using Vite:
2222

2323
``` cmd
24-
npx create-react-app dwt-react
24+
npm create vite@latest dwt-react --template react
2525
```
2626

27-
### **cd** to the root directory of the application and install the `dwt` and `ncp` package
27+
After running this, you will need to select the following options:
28+
- Select a framework: React
29+
- Select a variant: JavaScript
30+
31+
### Navigate to the project folder:
32+
33+
``` cmd
34+
cd dwt-react
35+
```
36+
37+
### Install dependencies and the required packages for DWT and NCP:
2838

2939
``` cmd
30-
yarn add dwt
40+
npm install
3141
```
3242

3343
``` cmd
34-
yarn add ncp
44+
npm install dwt ncp
3545
```
3646

3747
> `ncp` is used to copy static resources files.
@@ -42,10 +52,11 @@ Open `package.json` and change `scripts` like this:
4252

4353
``` json
4454
"scripts": {
45-
"start": "ncp node_modules/dwt/dist public/dwt-resources && react-scripts start",
46-
"build": "react-scripts build && ncp node_modules/dwt/dist build/dwt-resources",
47-
"test": "ncp node_modules/dwt/dist public/dwt-resources && react-scripts test",
48-
"eject": "react-scripts eject"
55+
"copy-resources": "ncp node_modules/dwt/dist public/dwt-resources",
56+
"dev": "npm run copy-resources && vite",
57+
"build": "npm run copy-resources && vite build",
58+
"lint": "eslint .",
59+
"preview": "vite preview"
4960
},
5061
```
5162

@@ -57,60 +68,58 @@ We can get these resource files in a few different ways. See our [resource loadi
5768

5869
### Generate a Component
5970

60-
Under `/src/`, create a new JavaScript file and name it `dwt.js`.
71+
Under `/src/`, create a new JavaScript file and name it `dwt.jsx`.
6172

6273
### Edit the Component
6374

64-
* Copy the following to the newly created `dwt.js`.
75+
* Copy the following to the newly created `dwt.jsx`.
6576

6677
``` typescript
67-
import React from 'react';
78+
import React, { useEffect, useRef } from 'react';
6879
import Dynamsoft from 'dwt';
6980

70-
export default class DWT extends React.Component {
71-
constructor(props) {
72-
super(props);
73-
}
74-
DWTObject = null;
75-
containerId = 'dwtcontrolContainer';
76-
componentDidMount() {
81+
function DWT() {
82+
const containerId = 'dwtcontrolContainer';
83+
const dwtObject = useRef(null);
84+
85+
useEffect(() => {
7786
Dynamsoft.DWT.RegisterEvent('OnWebTwainReady', () => {
78-
this.Dynamsoft_OnReady()
87+
dwtObject.current = Dynamsoft.DWT.GetWebTwain(containerId);
7988
});
89+
8090
Dynamsoft.DWT.ProductKey = 'YOUR-PRODUCT-KEY';
8191
Dynamsoft.DWT.ResourcesPath = "/dwt-resources";
8292
Dynamsoft.DWT.Containers = [{
8393
WebTwainId: 'dwtObject',
84-
ContainerId: this.containerId,
94+
ContainerId: containerId,
8595
Width: '300px',
8696
Height: '400px'
8797
}];
98+
8899
Dynamsoft.DWT.Load();
89-
}
90-
Dynamsoft_OnReady() {
91-
this.DWTObject = Dynamsoft.DWT.GetWebTwain(this.containerId);
92-
}
93-
acquireImage() {
94-
if (this.DWTObject) {
95-
this.DWTObject.SelectSourceAsync()
96-
.then(() => {
97-
return this.DWTObject.AcquireImageAsync({
98-
IfCloseSourceAfterAcquire: true,
99-
});
100-
})
101-
.catch((exp) => {
102-
console.error(exp.message);
100+
}, []);
101+
102+
const acquireImage = () => {
103+
if (dwtObject.current) {
104+
dwtObject.current.SelectSourceAsync()
105+
.then(() => dwtObject.current.AcquireImageAsync({
106+
IfCloseSourceAfterAcquire: true,
107+
}))
108+
.catch((error) => {
109+
console.error(error.message);
103110
});
104111
}
105-
}
106-
render() {
107-
return (<>
108-
<button onClick = {() => this.acquireImage()} > Scan </button>
109-
<div id = {this.containerId}> </div>
110-
</>
111-
);
112-
}
113-
}
112+
};
113+
114+
return (
115+
<>
116+
<button onClick={acquireImage}>Scan</button>
117+
<div id={containerId}></div>
118+
</>
119+
);
120+
};
121+
122+
export default DWT;
114123
```
115124

116125
> Note:
@@ -119,24 +128,27 @@ export default class DWT extends React.Component {
119128
> * `ProductKey` must be set to a valid trial or full key.
120129
> * `ResourcesPath` points to the location of the static files mentioned in [Configure the project](#configure-the-project).
121130
122-
### Add the Component to `App.js`
131+
### Add the Component to `App.jsx`
123132

124133
``` javascript
125134
import React from 'react';
126135
import './App.css';
127136
import DWT from './dwt';
128137

129138
function App() {
130-
return ( < DWT /> );
139+
return <DWT />;
131140
}
132141

133142
export default App;
134143
```
135144

145+
### remove default styles
146+
Clear the contents of the src/App.css and src/index.css files.
147+
136148
### Run the Application
137149

138150
``` cmd
139-
yarn start
151+
npm run dev
140152
```
141153

142154
> Note: If you have installed `DWT` and have configured a valid `ProductKey`, you will have a working page to scan documents from your scanner now. Otherwise, you should see instructions on the page that guide you to install the library. [More info>>]({{site.indepth}}features/initialize.html#installation-of-the-dynamsoft-service)

0 commit comments

Comments
 (0)