0

I am using React Native and want to store notes locally and I was searching out what will be the best way to store data locally. So, first I thought of sql but it's little bit complex then I went towards redux and it's easy to use. So, what will be the best solution for it for large number of data??

4

2 回答 2

0

Redux(或 MobX)和 AsyncStorage 将是在 iOS 和 Android 的 React-Native 中本地存储数据的最佳选择。

我个人建议您使用 Redux,因为它易于实现、使用和持久化(使用 AsyncStorage)并且可以很好地处理大量数据。

但是,如果可以在服务器端完成并且只在本地获取所需的数据,我不建议您在本地存储大量数据。

仅在本地存储必要的内容,并在需要时获取其余内容。当然,这一切都取决于您的应用程序和相关数据。但我的回答应该涵盖这样的一般用法。

于 2021-10-19T11:08:32.213 回答
0

这是一个关于如何使用 redux 持久化以及使用 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>
  )
}
于 2021-10-19T11:22:59.813 回答