我正在使用React Native,我想在本地存储笔记,我正在寻找在本地存储数据的最佳方式。所以,首先我想到了sql,但它有点复杂,然后我转向了redux,它很容易使用。那么,对于海量数据,最好的解决方案是什么呢?
发布于 2021-10-19 11:08:32
Redux (或MobX)和AsyncStorage将是在iOS和安卓的React-Native中本地存储数据的最佳选择。
我个人建议你使用Redux,因为它很容易实现、使用和持久化(使用AsyncStorage),并且可以很好地处理大量数据。
但是,我不建议您在本地存储大量数据,如果这可以在服务器端完成,并且只在本地获取所需的数据。
只在本地存储需要的内容,并在需要的时候获取其余的内容。当然,这完全取决于你的应用程序和相关的数据。但我的答案应该涵盖像这样的东西的一般用法。
发布于 2021-10-19 11:23:00
这是一个关于如何通过AsyncStorage使用redux persist和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://stackoverflow.com/questions/69629598
复制相似问题