ホームページ > 記事 > ウェブフロントエンド > React Native/ReactJS で遅延読み込みの概念をマスターする
遅延読み込み は、リソースが必要な場合にのみ読み込まれる設計パターンです。これは、React Native アプリケーションの初期ロード時間を短縮し、メモリ消費を削減し、全体的なパフォーマンスを向上させるのに役立ちます。
遅延ロード画面 (コード分割):
React Native では、通常、特にユーザーが頻繁にアクセスしない可能性のあるさまざまな画面がある場合に、コンポーネントに対して遅延読み込みが使用されます。これらの画面を遅延ロードすることで、初期バンドル サイズを削減できます。
React.lazy() と Suspense による遅延読み込み:
React では、コンポーネントの遅延読み込みを可能にする React.lazy() 関数が導入されました。遅延読み込みを使用するには、コンポーネントが読み込まれるまで、Suspense がフォールバックとして使用されます。
通常の使用法では、アプリの起動時にすべてのリソース、コンポーネント、ライブラリ、データが事前にロードされます。このアプローチは小規模なアプリケーションではうまく機能しますが、アプリが大きくなると非効率的でリソースが大量に消費され、パフォーマンスと読み込み時間に影響を与える可能性があります。
import React from 'react'; import HomeScreen from './screens/HomeScreen'; import ProfileScreen from './screens/ProfileScreen'; const App = () => { return ( <> <HomeScreen /> <ProfileScreen /> </> ); }; export default App;
説明:
遅延ロードでは、コンポーネント、ライブラリ、またはデータは必要な場合にのみロードされます。これにより、必要なリソースのみがオンデマンドでロードされるため、初期ロード時間とメモリ使用量が削減され、パフォーマンスが向上します。
import React, { Suspense, lazy } from 'react'; import { ActivityIndicator } from 'react-native'; const HomeScreen = lazy(() => import('./screens/HomeScreen')); const ProfileScreen = lazy(() => import('./screens/ProfileScreen')); const App = () => { return ( <Suspense fallback={<ActivityIndicator size="large" color="#0000ff" />}> <HomeScreen /> <ProfileScreen /> </Suspense> ); }; export default App;
説明:
Feature | Normal Usage | Lazy Loading |
---|---|---|
Loading Strategy | Everything is loaded upfront when the app starts. | Components, resources, or data are loaded only when needed. |
Initial Load Time | Higher, as all resources are loaded at once. | Lower, as only essential components are loaded upfront. |
Memory Usage | Higher, as all components and resources are loaded into memory. | Lower, as only necessary components are loaded into memory. |
User Experience | Slower startup but smoother transitions once loaded. | Faster startup but slight delay when loading resources. |
Best for | Small applications with limited components. | Large applications where not all components are used initially. |
Implementation | Simpler, as everything is bundled at once. | Requires managing dynamic imports and possibly loading states. |
Lazy loading ensures that screens or components are only mounted when they are accessed (when the user navigates to them), thus improving performance, especially in apps with multiple screens.
import React, { Suspense, lazy } from 'react'; import { createStackNavigator } from '@react-navigation/stack'; import { NavigationContainer } from '@react-navigation/native'; import { ActivityIndicator } from 'react-native'; // Lazy load screens const HomeScreen = lazy(() => import('./screens/HomeScreen')); const ProfileScreen = lazy(() => import('./screens/ProfileScreen')); const Stack = createStackNavigator(); const App = () => { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={() => ( <Suspense fallback={<ActivityIndicator size="large" color="#0000ff" />}> <HomeScreen /> </Suspense> )} /> <Stack.Screen name="Profile" component={() => ( <Suspense fallback={<ActivityIndicator size="large" color="#0000ff" />}> <ProfileScreen /> </Suspense> )} /> </Stack.Navigator> </NavigationContainer> ); }; export default App;
Explanation:
In React Native, lazy loading can be achieved using libraries like react-native-fast-image or manually handling image loading by tracking visibility with tools like IntersectionObserver.
react-native-fast-image is a performant image component that provides built-in lazy loading.
npm install react-native-fast-image
import React from 'react'; import { View, ScrollView, Text } from 'react-native'; import FastImage from 'react-native-fast-image'; const LazyLoadingImages = () => { return ( <ScrollView> <Text>Scroll down to load images</Text> <FastImage style={{ width: 200, height: 200 }} source={{ uri: 'https://example.com/my-image1.jpg', priority: FastImage.priority.normal, }} resizeMode={FastImage.resizeMode.contain} /> <FastImage style={{ width: 200, height: 200 }} source={{ uri: 'https://example.com/my-image2.jpg', priority: FastImage.priority.normal, }} resizeMode={FastImage.resizeMode.contain} /> </ScrollView> ); }; export default LazyLoadingImages;
Explanation:
In cases where you don't want to use a third-party library, you can implement lazy loading by tracking when an image enters the viewport using tools like IntersectionObserver (web) or a custom scroll listener in React Native.
import React, { useState, useEffect } from 'react'; import { View, Image, ScrollView } from 'react-native'; const LazyImage = ({ src, style }) => { const [isVisible, setIsVisible] = useState(false); const onScroll = (event) => { // Implement logic to determine if image is visible based on scroll position const { y } = event.nativeEvent.contentOffset; if (y > 100) { // Example: load image when scrolled past 100px setIsVisible(true); } }; return ( <ScrollView onScroll={onScroll} scrollEventThrottle={16}> <View> {isVisible ? ( <Image source={{ uri: src }} style={style} /> ) : ( <View style={style} /> )} </View> </ScrollView> ); }; const App = () => { return ( <LazyImage src="https://example.com/my-image.jpg" style={{ width: 200, height: 200 }} /> ); }; export default App;
Explanation:
When using Redux, you may want to lazy load certain reducers only when necessary, such as for specific screens or features.
Start by setting up a standard Redux store, but instead of adding all reducers upfront, create an injection method.
import { configureStore, combineReducers } from '@reduxjs/toolkit'; const staticReducers = { // Add reducers that are needed from the start }; export const createReducer = (asyncReducers = {}) => { return combineReducers({ ...staticReducers, ...asyncReducers, }); }; const store = configureStore({ reducer: createReducer(), }); // Store injected reducers here store.asyncReducers = {}; export default store;
In the above code:
Create a helper function to inject new reducers dynamically into the store.
// Helper function to inject a new reducer dynamically export function injectReducer(key, asyncReducer) { if (!store.asyncReducers[key]) { store.asyncReducers[key] = asyncReducer; store.replaceReducer(createReducer(store.asyncReducers)); } }
The injectReducer function checks if a reducer has already been added. If not, it injects it into the store and replaces the current root reducer.
Imagine you have a new page or feature that needs its own reducer. You can inject the reducer dynamically when this page is loaded.
import { lazy, Suspense, useEffect } from 'react'; import { useDispatch } from 'react-redux'; import { injectReducer } from './store'; import featureReducer from './features/featureSlice'; // The reducer for this feature const FeatureComponent = lazy(() => import('./components/FeatureComponent')); const FeaturePage = () => { const dispatch = useDispatch(); useEffect(() => { injectReducer('feature', featureReducer); // Dynamically load the reducer }, [dispatch]); return ( <Suspense fallback={<div>Loading...</div>}> <FeatureComponent /> </Suspense> ); }; export default FeaturePage;
Here:
The reducer for the feature is written as usual, using Redux Toolkit.
import { createSlice } from '@reduxjs/toolkit'; const featureSlice = createSlice({ name: 'feature', initialState: { data: [] }, reducers: { setData: (state, action) => { state.data = action.payload; }, }, }); export const { setData } = featureSlice.actions; export default featureSlice.reducer;
You might want to remove a reducer when it's no longer needed, for example, when navigating away from a page.
Here’s how you can remove a reducer:
export function removeReducer(key) { if (store.asyncReducers[key]) { delete store.asyncReducers[key]; store.replaceReducer(createReducer(store.asyncReducers)); } }
You can call this function when a feature or page is unmounted to remove its reducer from the store.
If you are using heavy third-party libraries, lazy loading them can help optimize performance.
import React, { useState } from 'react'; const HeavyComponent = React.lazy(() => import('heavy-library')); // React.lazy(() => import('moment')) const App = () => { const [showComponent, setShowComponent] = useState(false); return ( <View> <Button title="Load Heavy Component" onPress={() => setShowComponent(true)} /> {showComponent && ( <Suspense fallback={<Text>Loading...</Text>}> <HeavyComponent /> </Suspense> )} </View> ); };
Example: Lazy Loading Data:
import React, { useState, useEffect } from 'react'; import { FlatList, ActivityIndicator, Text } from 'react-native'; const LazyLoadData = () => { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetch('https://api.example.com/data') .then(response => response.json()) .then(json => { setData(json); setLoading(false); }); }, []); if (loading) { return <ActivityIndicator />; } return ( <FlatList data={data} renderItem={({ item }) => <Text>{item.name}</Text>} keyExtractor={item => item.id} /> ); }; export default LazyLoadData;
Explanation:
Lazy loading helps improve the performance, memory usage, and user experience of your React Native app, making it more efficient and responsive for users.
以上がReact Native/ReactJS で遅延読み込みの概念をマスターするの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。