Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parseInt() is not a function

Tags:

react-native

When I try to parseInt() it says:

_this2.state.grade.parseInt is not a function

Code:

this.state = {grade: "1"}

let grade = this.state.grade.parseInt()

Does this function not work in React Native? If so how can I convert it to int?

like image 749
Sinan Samet Avatar asked Apr 06 '26 13:04

Sinan Samet


2 Answers

Try:

Number.parseInt(this.state.grade, 10)
// the 2nd argument is the base mathematical
// system to use for parsing and should
// always be passed to avoid confusion and
// ensure predictable behaviour
like image 174
Pineda Avatar answered Apr 10 '26 00:04

Pineda


parseInt is a global function as part of ECMAScript 2015 wich might not be vailable.

Alternatively use:

Number.parseInt(value, base)

base is important because it result into different values :

Octadecimal

Number.parseInt( '011', 8)
-> 9

Decimal

Number.parseInt( '011', 10)
->11

Hexa Decimal

Number.parseInt( '011', 16)
    ->17
like image 21
Hitesh Sahu Avatar answered Apr 10 '26 01:04

Hitesh Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!