2016-11-27 156 views
-2

当我尝试parseInt函数(),它说:parseInt函数()不是一个函数

_this2.state.grade.parseInt不是一个函数

代码:

this.state = {grade: "1"} 

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

此功能在React Native中无效吗?如果是的话我怎么能把它转换为int?

+0

这是完全错误。 –

+0

this.state.grade是字符串“1”我想将其转换为数字1 –

+1

Try Number.parseInt(this.state.grade) – Pineda

回答

3

尝试:

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 
+2

您应该始终为parseInt指定'radix'参数。很可能你会想要解析10位数字,所以一个正确的解决方案是:'Number.parseInt(this.state.grade,10)'。有关参考,请参阅:https://davidwalsh.name/parseint-radix – jevakallio

+0

谢谢!不知道 –

+0

@DavidWalsh:谢谢。我相应地编辑了我的答案 – Pineda