简体   繁体   English

Agora React-native 功能组件实现

[英]Agora React-native functional component implementation

I am trying to implement agora video-call using functional component in react-native and getting the engine is not defined error.我正在尝试使用 react-native 中的功能组件实现 agora 视频通话,并得到引擎未定义错误。 I am new to react hooks, can someone help.我是反应钩子的新手,有人可以帮忙。 Below is the code for it: Declaring Engine -> let engine;下面是它的代码:Declaring Engine -> let engine;

const init = async() => {
    if (Platform.OS === 'android') {
      // Request required permissions from Android
      requestCameraAndAudioPermission().then(() => {
        console.log('requested!');
      });
    }
  }

  const createEngine = async() => {
    console.log("inside engine");
    try{
      console.log("inside try");
      engine = await RtcEngine.create(appId);
      //await setEngine(e);
      console.log(engine);
      await engine.enableVideo();
    }catch(e){
      console.log(e);
    }

useEffect(() => {
    init();
  },[]);

useEffect(() => {
  createEngine();
},[createEngine]);
The engine variable becomes undefined.

You are using asynchronous functions, so there is always going to be a period of time where the engine is undefined before it is ready.您正在使用异步函数,因此在引擎准备好之前总会有一段时间undefined engine The important thing is that your component is able to know whether or not you're got an engine and render itself accordingly.重要的是您的组件能够知道您是否拥有engine并相应地呈现自己。

Since engine is a variable that changes, you need to control it through a useState hook.由于engine是一个变化的变量,你需要通过一个useState钩子来控制它。 React will re-render your component whenever the state changes, but it can't do that with let engine .每当 state 更改时,React 都会重新渲染您的组件,但它不能用let engine做到这一点。 I am setting the initial value to undefined but it could be null or something else.我将初始值设置为undefined ,但它可能是null或其他东西。 We will call setEngine when the engine is ready.当引擎准备好时,我们将调用setEngine

const [engine, setEngine] = useState(undefined);

When declaring dependencies for a useEffect , you want to make sure that you are not depending on functions which get re-created on each render.在为useEffect声明依赖项时,您要确保不依赖在每次渲染时重新创建的函数。 You can use memoization techniques such as useCallback and useMemo , but here I am just moving the createEngine function inside the useEffect in order to avoid issues.您可以使用诸如useCallbackuseMemo类的记忆技术,但这里我只是将createEngine function 移动到useEffect中以避免出现问题。 You might want to do the same with init .您可能想对init做同样的事情。

export const MyComponent = ({ appId }) => {
    // engine state
    const [engine, setEngine] = useState(undefined);

    useEffect(() => {
        // variable used by cleanup function
        let isSubscribed = true;

        // create the function
        const createEngine = async () => {
            console.log("inside engine");
            try {
                console.log("inside try");
                const rtcEngine = await RtcEngine.create(appId);
                console.log(rtcEngine);
                await rtcEngine.enableVideo();
                // need to prevent calls to setEngine after the component has unmounted
                if (isSubscribed) {
                    setEngine(rtcEngine);
                }
            } catch (e) {
                console.log(e);
            }
        }

        // call the function
        createEngine();

        // return a cleanup
        return () => { isSubscribed = false; }

    },
        // will run once on component mount or if appId changes
        [setEngine, appId]
    );

    /* ... your init ... */

    return (
        <View>
            {engine // check if we have an engine and not undefined
                ?
                <SomeEngineComponent engine={engine} /> // if we know that we have an engine, we can do something with it
                :
                <SomeLoadingComponent /> // show a loading component while waiting for createEngine to finish 
            }
        </View>
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-Native 具有功能组件的 panResponder 阵列 - React-Native panResponder array with functional component react-native 如何将功能组件更改为类组件 - react-native how to change functional component to class component 如何在 react/react-native 中获取功能组件的节点处理程序 - How to get the node handler of a functional component in react/react-native 如何在反应原生功能组件中应用PropType? - how to apply PropType in react-native functional component? 在 react-native 功能组件中实现初始化代码的位置 - Where to implement initialization code in react-native functional component 在 react-native 功能组件中处理 401 响应 - Handling 401 responses in a react-native functional component 是否可以从 react-native 中的类组件访问功能上下文? - Is it possible to access functional context from class component in react-native? 如何从 react-native 中的另一个功能组件访问一个功能组件的状态? - How to access one functional component's state from another functional component in react-native? 我们如何在 react-native 功能组件中声明和使用 class 而不用 React.Component 扩展它? - How can we declare and use class in react-native functional component without extending it with React.Component? ckeditor在react-native中的实现 - ckeditor implementation in react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM