react-native - 如何实现 RAM bundle 和 Inline Requires react native
问题描述
我正在使用本机反应来构建项目,并且我面临启动时间长的问题,我尝试遵循https://reactnative.dev/docs/ram-bundles-inline-requires,但是关于调查并不清楚加载的模块,以及如何只为第一个屏幕放置必要的模块。我也找不到 index.(ios|android).js 文件(是 index.android.bundle)。如果你能告诉我如何只提取必要的模块并推荐有关实现它的文档或示例?
解决方案
With considering the official documents, pay attention to this example:
You have a Main screen
(view) in your app like HomeScreen
In your HomeScreen, there are more and more components and logic in this page but assume we have a SettingsModal
.
usually, modals
will open when you touch a button.
Without inline require
you have to import your modal component to your HomeScreen at the top level of your module
with inline require
you will import your modal component to your HomeScreen when it needs to show!
Let's do this with code:
HomeScreen without inline require
import React, {useState} from 'react'
import {View, Text, Pressable} from 'react-native'
import SettingsModal from 'components/modal'
function HomeScreen() {
const [showModal, setShowModal] = useState(false)
const handleShowModal = () => setShowModal(prevState => !prevState)
return (
<View>
<Text> Home Screen </Text>
<Pressable onPress={handleShowModal}>
<Text> show settings </Text>
</Pressable >
{
showModal
? <SettingsModal />
: null
}
</View>
)
}
In the above example, we import SettingsModal
in our HomeScreen top level with React and View and Text...
HomeScreen with inline require
import React, {useState} from 'react'
import {View, Text, Pressable} from 'react-native'
let SettingsModal = null;
function HomeScreen() {
const [showModal, setShowModal] = useState(false)
const handleShowModal = prevState => {
if (SettingsModal == null) {
SettingsModal = require('components/modal').SettingsModal
}
setShowModal(prevState => !prevState)
}
return (
<View>
<Text> Home Screen </Text>
<Pressable onPress={handleShowModal}>
<Text> show settings </Text>
</Pressable >
{
showModal
? <SettingsModal />
: null
}
</View>
)
}
In the above example, we check if SettingsModal
has not been imported yet, then we will import this component to our HomeScreen file (after user touch the show settings button)
推荐阅读
- mysql - 如何查找现在 5 分钟内的日期()
- linux - SSH 连接要求输入密码连接到 Docker 容器
- r - 将嵌套数据整理到数据框:不同类型的嵌套数据 - 逻辑列表和命名列表
- flutter - 将 Text 小部件添加到其子项映射到 Flutter 的 ListView?
- python - 在 Python 中使用十进制模块时如何抑制指数格式
- python - TypeError: 'function' 和 'int' 的实例之间不支持'>='
- php - 带树枝的 Symfony 动态页面
- cocoapods - XCode 12.3 为 iOS 模拟器构建,但在为 iOS 构建的 dylib 中链接,用于架构 arm64 的 couchbaselite 框架使用 M1 芯片组
- javascript - 构造函数内部回调内部变量的歧义
- c++ - 对特征矩阵执行布尔系数运算并返回找到的元素的索引