Skip to content

Commit

Permalink
remove watchKeys from the example, add note, update styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Simek committed Mar 10, 2020
1 parent 6c60465 commit 3ba2046
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions docs/settings.md
Expand Up @@ -8,30 +8,27 @@ title: Settings
## Example

```SnackPlayer name=Settings%20Example&supportedPlatforms=ios
import React, { useEffect, useState } from "react";
import React, { useState } from "react";
import { Button, Settings, StyleSheet, Text, View } from "react-native";
export default App = () => {
const [data, setData] = useState("-");
useEffect(() => {
const watchId = Settings.watchKeys(
"data",
() => setData(Settings.get("data"))
);
return () => Settings.clearWatch(watchId);
});
const [data, setData] = useState(Settings.get("data"));
const storeData = data => {
Settings.set(data);
setData(Settings.get("data"));
};
return (
<View style={styles.container}>
<Text>Stored value:</Text>
<Text>{data}</Text>
<Button
onPress={() => Settings.set({ data: "React" })}
<Text style={styles.value}>{data}</Text>
<Button
onPress={() => storeData({ data: "React" })}
title="Store 'React'"
/>
<Button
onPress={() => Settings.set({ data: "Native" })}
<Button
onPress={() => storeData({ data: "Native" })}
title="Store 'Native'"
/>
</View>
Expand All @@ -43,6 +40,10 @@ const styles = StyleSheet.create({
flex: 1,
justifyContent: "center",
alignItems: "center"
},
value: {
fontSize: 24,
marginVertical: 12
}
});
```
Expand All @@ -67,7 +68,7 @@ static clearWatch(watchId: number)
static get(key: string): mixed
```

Get the current value for a key in `NSUserDefaults`.
Get the current value for a given `key` in `NSUserDefaults`.

---

Expand All @@ -87,4 +88,6 @@ Set one or more values in `NSUserDefaults`.
static watchKeys(keys: string | array<string>, callback: function): number
```

Subscribe to be notified when the value for any of the keys specified by the `keys` array changes in `NSUserDefaults`. Returns a `watchId` number that may be used with `clearWatch()` to unsubscribe.
Subscribe to be notified when the value for any of the keys specified by the `keys` parameter has been changed in `NSUserDefaults`. Returns a `watchId` number that may be used with `clearWatch()` to unsubscribe.

> **Note:** `watchKeys()` by design ignores internal `set()` calls and fires callback only on changes preformed outside of React Native code.

0 comments on commit 3ba2046

Please sign in to comment.