zpao / qrcode.react

A <QRCode/> component for use with React.

Home Page:https://zpao.github.io/qrcode.react/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot read properties of undefined (reading 'lenght')

lucodifier opened this issue · comments

Error:

/node_modules/qrcode.react/lib/index.js:52
49 | function convertStr(str) {
50 | var out = '';
51 |

52 | for (var i = 0; i < str.length; i++) {
53 | var charcode = str.charCodeAt(i);
54 |
55 | if (charcode < 0x0080) {

Solution:

function convertStr(str: string): string {
let out = '';
if (str){
for (let i = 0; i < str.length; i++) {
let charcode = str.charCodeAt(i);
if (charcode < 0x0080) {
out += String.fromCharCode(charcode);
} else if (charcode < 0x0800) {
out += String.fromCharCode(0xc0 | (charcode >> 6));
out += String.fromCharCode(0x80 | (charcode & 0x3f));
} else if (charcode < 0xd800 || charcode >= 0xe000) {
out += String.fromCharCode(0xe0 | (charcode >> 12));
out += String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f));
out += String.fromCharCode(0x80 | (charcode & 0x3f));
} else {
// This is a surrogate pair, so we'll reconsitute the pieces and work
// from that
i++;
charcode =
0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
out += String.fromCharCode(0xf0 | (charcode >> 18));
out += String.fromCharCode(0x80 | ((charcode >> 12) & 0x3f));
out += String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f));
out += String.fromCharCode(0x80 | (charcode & 0x3f));
}
}
}
return out;
}

This should only get called with strings - what value are you passing into the qrcode?

If you're passing in null or undefined, then you aren't going to get a QRCode. I would strongly recommend checking that in your code before rendering.

I use the library Thanks zpao! but it's seems unmaintained.

if any interested, I have updated the library and migrate it to typescript https://github.com/devmehq/react-qr-code
if you have any suggestion or improvement and you could open there, i'm happy to help adding more features and improvements to the library! thanks folks.

No response, closing.