Tag Archives: Taro

Taro Use React Hooks Error: TypeError: Object(…) is not a function

  I stepped on a version of the pit today

In the taro project, you want to use react hooks. The code is as follows. Errors are always reported in the wechat development tool

After looking at the official documents, I found that taro was introduced after version 3.xx   Usestate and so on need to be introduced from the corresponding framework react

The code is modified as follows:

// React Hooks
import { useState, useEffect } from 'react' 
import { View, Text } from '@tarojs/components'
import './index.scss'
function Index() {
    const [userName, setUserName] = useState('Ashely')
    return (
        <View>
            <Text>{userName}</Text>
        </View>
    )
}

export default Index

You can compile normally

(node:2233) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, mkdir ‘/usr/loca~‘

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `–unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2233) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

When you encounter the above error when initializing a project with taro, this is due to insufficient permissions. Add sudo before the initialization command taro init, i.e.
Initialize command by:

taro init

Replace with:

sudo taro init

It can be created successfully!