小编jac*_*ack的帖子

React Native 打字稿数字 0 读为 NaN

我正在尝试映射一个数组并使用它的值或键作为我在 React Native (Android) 中返回的数据。\n任何人都可以解释为什么值 0 被读取为 NaN 吗?\n我在使用打字稿枚举。

\n

我正在使用:\ntypescript v.4.7.4、RN 0.69.2 和 React 18

\n

带有控制台输出的简单代码示例:

\n
const R = [...Array(4).keys()];\nR.map((_i, key) => {\n  console.log(_i, key);\n}); \n\n\nOutput: \nNaN NaN  // value 0 is interpreted as NaN ???\n1 1\n2 2\n3 3\n
Run Code Online (Sandbox Code Playgroud)\n

仅当在调试模式下通过 console.log 或 VSCode 检查器直接评估该值时,才会发生这种情况。每当我使用 \xc3\xa0 模板字符串时,该值都会正确显示。

\n
R.forEach(i => {\n  console.log(`value is : ${i} is ${typeof i}`);\n  console.log(i);\n});\n\nOutput: \nvalue is : 0 is number\nNaN\nvalue is : 1 is number\n1\nvalue is : 2 is number\n2\nvalue is : …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-native

9
推荐指数
1
解决办法
626
查看次数

标签 统计

react-native ×1

reactjs ×1

typescript ×1