我正在使用 React Native 并想在本地存盤筆記,我正在尋找在本地存盤資料的最佳方式。所以,首先我想到了 sql,但它有點復雜,然后我轉向了 redux,它很容易使用。那么,對于大量資料,它的最佳解決方案是什么?
uj5u.com熱心網友回復:
Redux(或 MobX)和 AsyncStorage 將是在 iOS 和 Android 的 React-Native 本地存盤資料的最佳選擇。
我個人建議您使用 Redux,因為它易于實作、使用和持久化(使用 AsyncStorage)并且可以很好地處理大量資料。
但是,如果可以在服務器端完成并且僅在本地獲取需要的資料,我不建議您在本地存盤大量資料。
只在本地存盤必要的內容,并在需要時獲取其余內容。當然,這一切都取決于您的應用程式和相關資料。但我的回答應該涵蓋這樣的一般用法。
uj5u.com熱心網友回復:
這是一個關于如何使用 redux persist 和使用 AsyncStorage 的 redux 的示例:
import 'react-native-gesture-handler'
import React from "react";
import AsyncStorage from '@react-native-async-storage/async-storage'
import { Provider } from 'react-redux'
import { createStore, compose, applyMiddleware } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
//store all your reducers here
import reducers from './src/Redux/reducers'
import { PersistGate } from 'redux-persist/lib/integration/react'
import createSagaMiddleware from 'redux-saga';
//React navigation
import Navigation from "./src/Navigation";
import rootSaga from './src/Redux/Sagas';
const sagaMiddleware = createSagaMiddleware()
const persistConfig = {
key: 'root',
storage: AsyncStorage,
//an array that has whichever reducers you want to persist
whitelist: ['BananaReducer'],
}
const persistedReducer = persistReducer(persistConfig, reducers)
const store = createStore(
persistedReducer,
{},
compose(applyMiddleware(sagaMiddleware)),
)
sagaMiddleware.run(rootSaga)
export default function App() {
const persistor = persistStore(store)
return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Navigation />
</PersistGate>
</Provider>
)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328246.html
標籤:反应原生 还原持久化 react-native-sqlite-storage
上一篇:反應識別控制或按下Shift鍵
下一篇:2021-10-20
