react-native - 如何在从关闭更改为打开时检查 wifi 状态
问题描述
我想使用本机模块在 react-native 中使用服务,如果 Wifi 连接从关闭更改为打开,只需显示连接更改的 toast
解决方案
您可以使用react-native-netinfo插件。安装此插件,然后将以下代码添加到您的文件中:
import NetInfo from '@react-native-community/netinfo';
...
...
class App extends Component {
netInfoHandler;
constructor(props) {
super(props);
this.state = {
isConnected: true
}
}
componentDidMount = () => {
this.netInfoHandler = NetInfo.addEventListener(async (state) => {
if (this.state.isConnected !== state.isConnected) {
this.setState({ isConnected: state.isConnected });
// show toast here
}
});
}
componentWillUnmount = () => {
this.netInfoHandler();
}
render() {
return (
...
// your view
)
}
}
export default App
推荐阅读
- c# - 在 C# 或 Vb 中制作圆形按钮(Windows 窗体)
- javascript - 我可以在 chrome 开发工具中模拟我的请求的响应吗?
- c++ -
返回 static_cast指针导致的 C++ 双重释放 - html - 如何修复:我的背景在菜单下
- swift - 为什么“didSelectRowAt indexPath”函数只在两指点击后运行?
- javascript - 每当我进入任何网页时运行 JavaScript 代码
- vb.net - 当只允许输入字母时,如何让两个不同的文本框确定是否输入了数字?
- java - 如何使用 okhttp3 发布包含图像路径的 JSON 字符串
- javascript - 为什么我的模态内容的背景颜色不显示?
- javascript - 检测垂直滚动是否可见