react-native-mmkv provides an easy to use React-Hooks API to be used in Function Components.
function App() {
const [username, setUsername] = useMMKVString("user.name")
const [age, setAge] = useMMKVNumber("user.age")
const [isPremiumUser, setIsPremiumUser] = useMMKVBoolean("user.isPremium")
const [privateKey, setPrivateKey] = useMMKVBuffer("user.privateKey")
}
function App() {
const [username, setUsername] = useMMKVString("user.name")
// ...
const onLogout = useCallback(() => {
setUsername(undefined)
}, [])
}
type User = {
id: string
username: string
age: number
}
function App() {
const [user, setUser] = useMMKVObject<User>("user")
}
function App() {
const storage = useMMKV()
// ...
const onLogin = useCallback((username) => {
storage.set("user.name", "Marc")
}, [storage])
}
function App() {
const globalStorage = useMMKV()
const userStorage = useMMKV({ id: `${userId}.storage` })
const [username, setUsername] = useMMKVString("user.name", userStorage)
}
function App() {
useMMKVListener((key) => {
console.log(`Value for "${key}" changed!`)
})
}
function App() {
const storage = useMMKV({ id: `${userId}.storage` })
useMMKVListener((key) => {
console.log(`Value for "${key}" changed in user storage!`)
}, storage)
}