在 React Native 中使用上下文 [英] Using context in React Native

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

问题描述

在基于 web 的 React 中,我们的 props 有上下文,可以设置并用作依赖注入机制.我想在 React Native 应用程序中做类似的事情.有没有办法做到这一点?

解决方案

React Native 使用与 Web 应用程序相同的 react 包.所有 React Native 组件的工作方式都与 React 组件相同(在创建、生命周期和 API 方面,主要区别在于它们不渲染 HTML 元素,而是与 OS API 通信的自定义组件),因此您可以像在 React 中一样使用上下文.事实上,F8 应用程序利用上下文功能来处理 Android 中的后退按钮.查看源代码.>

In web based React our props have context, which can be set and in used as dependency injection mechanism. I would like to do something similar in React native app. Is there any way to do that?

解决方案

React Native uses the same react package used for web applications. All React Native components work in the same way as the React ones (in terms of creation, lifecycle and the API, the main difference being that they do not render HTML elements, but custom components that then talk to the OS API), so you can use context in the same way you do in React. In fact, the F8 App makes use of the context functionality in order to handle the back button in Android. Have a look at the source code.

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

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