From bc078905be6a2e319d08aa790d2effb59801fa4a Mon Sep 17 00:00:00 2001 From: rnigro-rwb <89093814+rnigro-rwb@users.noreply.github.com> Date: Wed, 21 May 2025 10:59:42 -0400 Subject: [PATCH] docs: Add example for Hooks with default value --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 99a27708..15de9d0e 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,19 @@ const [age, setAge] = useMMKVNumber('user.age') const [isMmkvFastAsf, setIsMmkvFastAf] = useMMKVBoolean('is-mmkv-fast-asf') ``` +To avoid the possibility of the variable being `undefined` it can be given a default value which will be used in the case that a value is not found for the given `key`. + +```js +// default to 'Marc' if 'user.name' key is not found +const [username = 'Marc', setUsername] = useMMKVString('user.name') + +// default to 21 if 'user.age' is not found +const [age = 21, setAge] = useMMKVNumber('user.age') + +// default to true if 'is-mmkv-fast-asf' is not found +const [isMmkvFastAsf = true, setIsMmkvFastAf] = useMMKVBoolean('is-mmkv-fast-asf') +``` + ### Keys ```js