typescript 在React中将useEffect与异步函数一起使用时出现Babel错误

pkln4tw6  于 2022-11-26  发布在  TypeScript
关注(0)|答案(1)|浏览(117)

我正在尝试调用API并使用useEffect更新React Typescript项目中的状态。
基本项目在“react-script@4.0.3”和typescript@latest上运行
我正在尝试将新组件添加到Uniswap存储库-〉https://github.com/Uniswap/interface
这是我的组件

const { account, chainId, provider } = useWeb3React()
  const [iconClicked, setIconCLicked] = useState(false)
  const [notifications, setNotification] = useState<notifI[]>([])
  const [showWindow, setShowWindow] = useState(false)
  const fetchData = async () => {
    try {
      const data: notifI[] = await PushAPI.user.getFeeds({
        user: `eip155:${chainId}:${account}`,
        env: 'staging',
      })
      setNotification(data)
    } catch (e) {
      console.log(e)
    }
  }
  useEffect(() => {
    if (account) {
      console.log('Calling user details')
      fetchData()
    }
  }, [account])

我得到的错误

./node_modules/@ethereumjs/util/dist/asyncEventEmitter.js 24:41
Module parse failed: Unexpected token (24:41)
File was processed with these loaders:
 * ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|     let [data, callback] = args;
|     const self = this;
>     let listeners = self._events[event] ?? [];
|     // Optional data argument
|     if (callback === undefined && typeof data === 'function') {

我真的是新的工作与这样的巨大的存储库,请帮助我,并提前感谢

tsm1rwdh

tsm1rwdh1#

在useEffect中移动/定义fetchData函数
有关详细信息,请参阅:this blog

相关问题