是否有任何部分应该以不同的方式看待"react-native-mmkv"和"redux"?对于反应原生



在我的项目中我同时使用'react-native-mmkv'和'redux',但是数据的存储和使用方式没有太大的区别。除了性能之外,如何更好地安全地存储和使用数据?还是两种方法都用更好?

我很抱歉,我不擅长英语。

是我的代码

import * as React from 'react';
import {
SafeAreaView,
Text,
Pressable,
} from 'react-native';
// UI components
import { Button } from 'atoms';
import { Card, SelectList } from 'molecules';
// Hooks
import { useIsFocused } from '@react-navigation/native';
// Redux
import { useAppSelector, useAppDispatch } from 'hooks';
// utils
import { initializeAccountData } from 'utils';
import { useMMKVString } from "react-native-mmkv";
const App = ({ navigation, route }: any) => {
// Navigation
const isFocused = useIsFocused();
// Redux
// const { accounts } = useAppSelector(state => state.accounts);
// const dispatch = useAppDispatch();
// MMKV Hooks
const [accounts, setAccount] = useMMKVString('accounts')
// Update to didMount, update
React.useEffect(() => {
const dataInitialize = async () => {
await initializeAccountData();
}
dataInitialize();
}, []);

// Update to page focusing
React.useEffect(() => {
}, [isFocused]);
// Update to account change
React.useEffect(() => {
}, [accounts]);
return (
<SafeAreaView style={{ flex: 1 }}>
...
</SafeAreaView>
)
}
export default App;

Redux是在内存存储,如果你杀死你的应用程序,你将失去应用程序的状态。

MMKV是持久存储,如果你想长期保存数据,你需要使用它。

如果您想存储redux状态,可以将MMKV与redux连接。示例如下https://github.com/mrousavy/react-native-mmkv/blob/master/docs/WRAPPER_REDUX.md

最新更新