加载中...
  • _tarojs_taro__WEBPACK_IMPORTED_MODULE_1__.default.useState is not a function

    在Taro中我希望在页面中使用react提供的 hooks 函数式编程,然后就报错

    1
    Uncaught TypeError: _tarojs_taro__WEBPACK_IMPORTED_MODULE_1__.default.useState is not a function

    这里先要说明的是,我确定当前的版本已经是最新版本,也支持hooks编写,这里目前是这样写的

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import Taro, {  useState } from '@tarojs/taro'
    import { View, Text } from '@tarojs/components'

    function Index(){
    const [userName ,setUserName] = useState('Hello World!!!!')

    return (
    <View>
    <Text>{userName}</Text>
    </View>
    )
    }

    export default Index

    然后就报了上面的错误

    有人说需要更改版本升级到1.3.0-beta.3 并且peerDependencies “nervjs”: “^1.4.0-beta.4” 。就可以了。
    我没有这样试过

    解决方案,使用react本身所提供的hooks

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import React, { useState } from 'react'
    import { View, Text } from '@tarojs/components'

    function Index(){
    const [userName ,setUserName] = useState('Hello World!!!!')

    return (
    <View>
    <Text>{userName}</Text>
    </View>
    )
    }

    export default Index

    这样就解决了,测试小程序跟h5都无异常

    说明:刚接触taro,在一步步踩坑,如果写的哪里有不足之处,莫怪莫怪哈

    上一篇:
    react子组件向父组件传值
    下一篇:
    taro如何获取路径传参
    本文目录
    本文目录