Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
61 views
in Technique[技术] by (71.8m points)

javascript - passing my function to useeffect crashes my app

Hi Im trying to implement openpay services to my react native app but when i try to generate the TokenID from the credit card info i got problems with the use effect

this is the code

 useEffect(() => {
let isCancelled = false;
if (vCardNameOwner && vCardNumber && vCardExpirationMonth && vCardExpirationYears && vCardCVV) {
openpay.createCardToken({
    holder_name: vCardNameOwner,
    card_number: vCardNumber,
    expiration_month: vCardExpirationMonth,
    expiration_year: vCardExpirationYears,
    cvv2: vCardCVV,
   })
      .then((token) => setvTokenId(token), console.log( '/////generacion de token//////' ,token));
    }
      return () => {
        isCancelled = true;
      };
  }, [vCardNameOwner, vCardNumber, vCardExpirationMonth, vCardExpirationYears, vCardCVV]);

that code makes my app to crash because token isnt declared and i dont know how to re do the operation so it can work fine

if i delete the useeffect thing and leave just the openpay function with hardcoded values everything work well but i need it to take the values that the user insert in the form

 openpay.createCardToken({
    holder_name: vCardNameOwner,
    card_number: vCardNumber,
    expiration_month: vCardExpirationMonth,
    expiration_year: vCardExpirationYears,
    cvv2: vCardCVV,
  })
  .then((token) => setvTokenId(token), console.log( '/////generacion de token//////' ,token));

thanks a lot for your time

question from:https://stackoverflow.com/questions/65889636/passing-my-function-to-useeffect-crashes-my-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems to be a syntax error.

This will trigger an error as it is not in the instruction block

, console.log( '/////generacion de token//////' ,token)

If you just want to console log the token, you can do it like this.

openpay.createCardToken({
    holder_name: vCardNameOwner,
    card_number: vCardNumber,
    expiration_month: vCardExpirationMonth,
    expiration_year: vCardExpirationYears,
    cvv2: vCardCVV,
  })
  .then((token) => console.log( '/////generacion de token//////' ,token) ||?setvTokenId(token));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...