React Native 中的 viewWillAppear [英] viewWillAppear in React Native

查看:120
本文介绍了React Native 中的 viewWillAppear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 React Native 在 View 中流式传输来自相机的视频.问题是,如果用户按下主页按钮,暂时离开应用并切换回来,流 URL 将被更改.

I'm trying to stream video from the camera in a View with React Native. The problem is, if the user presses the home button, temporarily leave the app and switch back, the stream URL will be changed.

所以我需要在一个类似于 viewWillAppear 的函数中更新 URL,就像在原生 iOS 框架中一样.对此有什么建议吗?

So I need to update the URL in a function similar to viewWillAppear as in native iOS framework. Any suggestion on this?

推荐答案

是的 @Danny Want 是对的,您可以使用 AppState.可以这样使用:

Yes @Danny Want is right you can use AppState. It can be used like this:

getInitialState: function() {
  return {
    currentAppState: AppState.currentState,
  };
},
componentDidMount: function() {
  AppState.addEventListener('change', this._handleAppStateChange);
},
componentWillUnmount: function() {
  AppState.removeEventListener('change', this._handleAppStateChange);
},
_handleAppStateChange: function(currentAppState) {
  this.setState({ currentAppState, });
},
render: function() {
  return (
    <Text>Current state is: {this.state.currentAppState}</Text>
  );
},

AppState 处理程序中做你想做的事:_handleAppStateChange.有关更多详细信息,请查看此处

Do what you wanna do in the AppState handler: _handleAppStateChange. For more details please check out here

这篇关于React Native 中的 viewWillAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆