Ionic 无法找到名称“webmaster”

soat7uwm  于 2023-09-28  发布在  Ionic
关注(0)|答案(1)|浏览(145)

我在React应用程序中初始化CometChat时遇到了问题。
我得到的错误是:

Error: Cannot find name 'setCometChat'. Did you mean 'CometChat'?
'CometChat' is declared here.

我有一个useEffect用于初始化:

useEffect(() => {
  initCometChat();
  // ...
}, []);

下面是initCometChat函数:

const initCometChat = async () => {
  const { CometChat } = await import('@cometchat-pro/cordova-ionic-chat');
  const appID = `${process.env.REACT_APP_COMETCHAT_APP_ID}`;
  const region = `${process.env.REACT_APP_COMETCHAT_REGION}`;
  const appSetting = new CometChat.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(region).build();
  CometChat.init(appID, appSetting).then(
    () => {
      console.log('CometChat was initialized successfully');
      setCometChat(() => CometChat);
    },
    error => {
    }
  );
};

我试着像这样使用传入的setter函数:

setCometChat(CometChat);

但没有用。

ecfdbz9o

ecfdbz9o1#

不熟悉Ionic,但如果我理解正确的话,您正在尝试将CometChat的当前示例设置为您一直用于初始化的变量CometChat。我认为错误的发生是因为CometChat没有被初始化为使用useState()钩子的状态。
但在本例中,将CometChat状态设置为用于初始化的变量CometChat不应该起作用。也许可以尝试将正在初始化的CometChatCometChat本身分开。我的意思是:

import React, { useState, useEffect } from 'react';

function Component() {
  const [cometChatInitialized, setCometChatInitialized] = useState(null);

  const initCometChat = async () => {
    const { CometChat } = await import('@cometchat-pro/cordova-ionic-chat');
    // ...

    CometChat.init(appID, appSetting).then(
      () => {
        console.log('CometChat was initialized successfully');
        setCometChatInitialized(CometChat);
      },
      // ...
    );
  };
}

相关问题