_tarojs_taro__WEBPACK_IMPORTED_MODULE_1__.default.useState is not a function

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

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

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

import Taro, {  useState } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'

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

  return ( 
    
        {userName}
    
  )
}

export default Index

然后就报了上面的错误

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

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

import React, { useState } from 'react'
import { View, Text } from '@tarojs/components'

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

  return ( 
    
        {userName}
    
  )
}

export default Index

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

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

你可能感兴趣的:(_tarojs_taro__WEBPACK_IMPORTED_MODULE_1__.default.useState is not a function)