Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 579 Bytes

LISTENERS.md

File metadata and controls

29 lines (21 loc) · 579 Bytes

Listeners

MMKV instances also contain an observer/listener registry.

Add a listener when a key's value changes.

const storage = new MMKV()

const listener = storage.addOnValueChangedListener((changedKey) => {
  const newValue = storage.getString(changedKey)
  console.log(`"${changedKey}" new value: ${newValue}`)
})

Don't forget to remove the listener when no longer needed. For example, when the user logs out:

function SettingsScreen() {
  // ...

  const onLogout = useCallback(() => {
    // ...
    listener.remove()
  }, [])

  // ...
}