[英]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.您可以使用诸如
useCallback
和useMemo
类的记忆技术,但这里我只是将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.