Comments (8)
If someone is having trouble accessing the underlying instance and registering the event with the hooks API here is the working TS code:
const [signaturePad, setSignaturePad] = React.useState<SignaturePad>();
const signaturePadRef = React.useCallback((signaturePad: SignaturePad) => {
setSignaturePad(signaturePad);
}, []);
React.useEffect(() => {
if (signaturePad) {
signaturePad.instance.onEnd = () => {
setSignature(signaturePad.instance.toDataURL());
};
}
}, [signaturePad]);
More info of why this is quite tricky can be found here or here.
from react-signature-pad-wrapper.
You are right. The following will work:
const [signaturePad, setSignaturePad] = React.useState<SignaturePad>();
const signaturePadRef = React.useCallback((signaturePad: SignaturePad) => {
setSignaturePad(signaturePad);
}, []);
const onStrokeEnd = () => {
setSignature(signaturePad.toData());
}
React.useEffect(() => {
if (signaturePad) {
signaturePad.instance.addEventListener('endStroke', onStrokeEnd)
}
//cleanup event listener
() => signaturePad.instance.removeEventListener('endStroke', onStrokeEnd);
}, [signaturePad]);
from react-signature-pad-wrapper.
You can already get the original signature pad instance through a ref (e.g. see Layout.tsx in the example dir). I think that should allow you to do what you want?
from react-signature-pad-wrapper.
Ah perfect! The instance
field it is.. Thx!
from react-signature-pad-wrapper.
The above .onEnd no longer works as it was replaced by event emitters. See here
The following updated code works:
const [signaturePad, setSignaturePad] = React.useState<SignaturePad>();
const signaturePadRef = React.useCallback((signaturePad: SignaturePad) => {
setSignaturePad(signaturePad);
}, []);
React.useEffect(() => {
if (signaturePad) {
signaturePad.instance.addEventListener('endStroke', () => {
setSignature(signaturePad.toDataURL());
});
}
}, [signaturePad]);
from react-signature-pad-wrapper.
Nice, thanks for the example!
I have to upgrade my code, but I suspect that it is necessary to remove the event listener in order to not reregister the event handler on every mount?
from react-signature-pad-wrapper.
@JamesRobertWiseman
Have you tested the code?
from react-signature-pad-wrapper.
@sankarkumar23 Yes, and it is working fine my end.
from react-signature-pad-wrapper.
Related Issues (20)
- Reference declarations in package.json
- Upgrade signature pad to v4
- Issue with source-map-loader - Failed to parse source map from 'XXX\node_modules\react-signature-pad-wrapper\src\SignaturePad.tsx' HOT 4
- Component no longer works with typescript since v3.1.0 HOT 5
- Running example HOT 2
- How to assign the value back to signature pad? HOT 4
- Support React 18
- Replace Enzyme with React Testing Library
- Signature with browser Zoom In and Zoom out HOT 1
- [Feature] Missing option in fromDataUrl function
- Find a replacement for rollup-plugin-terser plugin
- Store the signature without any background HOT 1
- Signature Pad Flickering
- Signature Pad Flickering Issue HOT 1
- Help with isEmpty function HOT 3
- Should we move throttle-debounce to dependencies? HOT 2
- Save signature as svg or png HOT 1
- Missing `ref` property in the listed properties
- Please donβt use class'es HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-signature-pad-wrapper.