Skip to content

Commit 678e868

Browse files
authored
docs: update examples to use newer React syntax (#119)
- use `createRoot` for `react-dom` rendering - use functional components with hooks syntax - add link to `ref` docs - consistently use `sigCanvas` as name - don't switch b/t `sigPad` and `sigCanvas` - consistently format `example/` dir with `ts-standard` - was previously ignored for less diff, but since the example is re-written with newer syntax, good timing to reformat it too
1 parent 6bcb3da commit 678e868

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

Diff for: README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ npm i -S react-signature-canvas
3535

3636
## Usage
3737

38-
```javascript
38+
```jsx
3939
import React from 'react'
40-
import ReactDOM from 'react-dom'
40+
import { createRoot } from 'react-dom/client'
4141
import SignatureCanvas from 'react-signature-canvas'
4242

43-
ReactDOM.render(
43+
createRoot(
44+
document.getElementById('my-react-container')
45+
).render(
4446
<SignatureCanvas penColor='green'
4547
canvasProps={{width: 500, height: 200, className: 'sigCanvas'}} />,
46-
document.getElementById('react-container')
4748
)
4849
```
4950

@@ -80,10 +81,17 @@ Of these props, all, except for `canvasProps` and `clearOnResize`, are passed th
8081

8182
### API
8283

83-
All API methods require a ref to the SignatureCanvas in order to use and are instance methods of the ref.
84+
All API methods require [a ref](https://react.dev/learn/manipulating-the-dom-with-refs) to the SignatureCanvas in order to use and are instance methods of the ref.
8485

85-
```javascript
86-
<SignatureCanvas ref={(ref) => { this.sigCanvas = ref }} />
86+
```jsx
87+
import React, { useRef } from 'react'
88+
import SignatureCanvas from 'react-signature-canvas'
89+
90+
function MyApp() {
91+
const sigCanvas = useRef(null);
92+
93+
return <SignatureCanvas ref={sigCanvas} />
94+
}
8795
```
8896

8997
- `isEmpty()` : `boolean`, self-explanatory

Diff for: example/src/index.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,36 @@
1-
import React, { Component } from 'react'
1+
import React, { useState, useRef } from 'react'
22
import { createRoot } from 'react-dom/client'
33

4-
import SignaturePad from '../../src/index.tsx'
4+
import SignatureCanvas from '../../src/index.tsx'
55

66
import * as styles from './styles.module.css'
77

8-
class App extends Component {
9-
state = { trimmedDataURL: null }
8+
function App () {
9+
const sigCanvas = useRef(null)
10+
const [trimmedDataURL, setTrimmedDataURL] = useState(null)
1011

11-
sigPad = {}
12-
13-
clear = () => {
14-
this.sigPad.clear()
12+
function clear () {
13+
sigCanvas.current.clear()
1514
}
1615

17-
trim = () => {
18-
this.setState({
19-
trimmedDataURL: this.sigPad.getTrimmedCanvas().toDataURL('image/png')
20-
})
16+
function trim () {
17+
setTrimmedDataURL(sigCanvas.current.getTrimmedCanvas().toDataURL('image/png'))
2118
}
2219

23-
render () {
24-
const { trimmedDataURL } = this.state
25-
return <div className={styles.container}>
20+
return (
21+
<div className={styles.container}>
2622
<div className={styles.sigContainer}>
27-
<SignaturePad canvasProps={{ className: styles.sigPad }}
28-
ref={(ref) => { this.sigPad = ref }} />
23+
<SignatureCanvas canvasProps={{ className: styles.sigCanvas }} ref={sigCanvas} />
2924
</div>
3025
<div>
31-
<button className={styles.buttons} onClick={this.clear}>
32-
Clear
33-
</button>
34-
<button className={styles.buttons} onClick={this.trim}>
35-
Trim
36-
</button>
26+
<button className={styles.buttons} onClick={clear}>Clear</button>
27+
<button className={styles.buttons} onClick={trim}>Trim</button>
3728
</div>
3829
{trimmedDataURL
39-
? <img className={styles.sigImage} alt='signature'
40-
src={trimmedDataURL} />
30+
? <img className={styles.sigImage} alt='signature' src={trimmedDataURL} />
4131
: null}
4232
</div>
43-
}
33+
)
4434
}
4535

4636
createRoot(document.getElementById('container')).render(<App />)

Diff for: example/src/styles.module.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ body {
1919
background-color: #fff;
2020
}
2121

22-
.sigPad {
22+
.sigCanvas {
2323
width: 100%;
2424
height: 100%;
2525
}

Diff for: package.json

-5
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,5 @@
112112
"jest-environment-jsdom": {
113113
"canvas": "$canvas"
114114
}
115-
},
116-
"ts-standard": {
117-
"ignore": [
118-
"example"
119-
]
120115
}
121116
}

0 commit comments

Comments
 (0)