Giter VIP home page Giter VIP logo

Comments (8)

toburger avatar toburger commented on September 10, 2024 1

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.

JamesRobertWiseman avatar JamesRobertWiseman commented on September 10, 2024 1

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.

michaeldzjap avatar michaeldzjap commented on September 10, 2024

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.

toburger avatar toburger commented on September 10, 2024

Ah perfect! The instance field it is.. Thx!

from react-signature-pad-wrapper.

JamesRobertWiseman avatar JamesRobertWiseman commented on September 10, 2024

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.

toburger avatar toburger commented on September 10, 2024

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.

sankarkumar23 avatar sankarkumar23 commented on September 10, 2024

@JamesRobertWiseman
Have you tested the code?

from react-signature-pad-wrapper.

JamesRobertWiseman avatar JamesRobertWiseman commented on September 10, 2024

@sankarkumar23 Yes, and it is working fine my end.

from react-signature-pad-wrapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.