Skip to content

Commit

Permalink
Merge branch 'master' into chore/grammar-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed authored Aug 16, 2024
2 parents ac7316a + ad6d247 commit d03e59a
Show file tree
Hide file tree
Showing 7 changed files with 1,374 additions and 537 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
src/
!dist/src
!android/src

index.ts

jest/memoryStore.ts
Expand Down
14 changes: 14 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@ repositories {
android {
compileSdkVersion safeExtGet("compileSdkVersion", 31)

def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
namespace "com.ammarahmed.mmkv"
}

if (rootProject.hasProperty("ndkPath")) {
ndkPath rootProject.ext.ndkPath
}
if (rootProject.hasProperty("ndkVersion")) {
ndkVersion rootProject.ext.ndkVersion
}

buildFeatures {
prefab true
}



defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 19)
targetSdkVersion safeExtGet('targetSdkVersion', 31)
Expand Down
3 changes: 2 additions & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Querying and Indexing](queryingandindexing.md)
- [useMMKVStorage](usemmkvstorage.md)
- [useIndex](useindex.md)
- [Transaction Manager](transactionmanager.md)
- [useMMKVRef](usemmkvref.md)
- [Transcation Manager](transactionmanager.md)

- [Changelog](changelog.md)
133 changes: 3 additions & 130 deletions docs/callbackapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,8 @@ Sets a string value in storage for the given key.
|----------|----------|
| key | String |
| value | String |
| callback | Function |

```js
// WITH CALLBACK
MMKV.setString("string", "string", (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result);
});

// WITHOUT CALLBACK
MMKV.setString("string", "string");
```

Expand All @@ -50,21 +38,9 @@ Gets a string value for a given key.
| Name | Type |
|----------|----------|
| key | String |
| callback | Function |

```js
MMKV.getString("string", (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); // Logs 'string';
});

// WITHOUT CALLBACK

let string = MMKV.getString("string");
MMKV.getString("string");
```

**Returns**
Expand All @@ -83,16 +59,6 @@ Sets a number value in storage for the given key.
| callback | Function |

```js
MMKV.setInt("number", 10, (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); //Logs true;
});

// WITHOUT CALLBACK
MMKV.setInt("number", 10);
```

Expand All @@ -108,17 +74,7 @@ Gets a number value for a given key.
| callback | Function |

```js
MMKV.getInt("number", (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); // logs 10;
});

// WITHOUT CALLBACK
let number = MMKV.getInt("number");
MMKV.getInt("number");
```

**Returns**
Expand All @@ -137,17 +93,6 @@ Sets a boolean value in storage for the given key.
| callback | Function |

```js
MMKV.setBool("boolean", true, (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); // logs true;
});

// WITHOUT CALLBACK

MMKV.setBool("boolean", true);
```

Expand All @@ -163,18 +108,7 @@ Gets a boolean value for a given key.
| callback | Function |

```js
MMKV.getBool("boolean", (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); // logs true;
});

// WITHOUT CALLBACK

let boolean = MMKV.getBool("boolean");
MMKV.getBool("boolean");
```

**Returns**
Expand All @@ -198,17 +132,6 @@ let object = {
bar: "bar",
};

MMKV.setMap("object", object, (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); // logs true;
});

// WITHOUT CALLBACK

MMKV.setMap("object", object);
```

Expand All @@ -224,19 +147,6 @@ Gets an object from storage.
| callback | Function |

```js
MMKV.getMap("object", (error, result) => {
if (error) {
console.log(error);
return;
}

let object = result;

console.log(object); // logs object
});

// WITHOUT CALLBACK

let object = MMKV.getMap("object");
```

Expand All @@ -258,17 +168,6 @@ Sets an array to storage for the given key.
```js
let array = ["foo", "bar"];

MMKV.setArray("array", array, (error, result) => {
if (error) {
console.log(error);
return;
}

console.log(result); // logs true
});

// WITHOUT CALLBACK

MMKV.setArray("array", array);
```

Expand All @@ -284,19 +183,6 @@ Sets an array to storage for the given key.
| callback | Function |

```js
MMKV.getArray("array", (error, result) => {
if (error) {
console.log(error);
return;
}

let array = result;

console.log(array); // logs array
});

// WITHOUT CALLBACK

let array = MMKV.getArray("array");
```

Expand All @@ -318,19 +204,6 @@ Retrieve multiple Objects for a given array of keys. **Currently will work only
```js
import MMKV from "react-native-mmkv-storage";

MMKV.getMultipleItems(["foo", "bar", "loo"], "map", (error, result) => {
if (error) {
console.log(error);
return;
}

let objects = result;

console.log(objects); // logs objects
});

// WITHOUT CALLBACK

let items = MMKV.getMultipleItems(["foo", "bar", "loo"], "map");
```

Expand Down
63 changes: 63 additions & 0 deletions docs/usemmkvref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# useMMKVRef hook

A persisted ref that will always write every change to it's `current` value to storage. It doesn't matter if you reload the app or restart it. Everything will be in place on app load. Let's see how this works:

### `useMMKVRef`
A `useRef` like hook that allows you to easily manage values in storage.

**Arguments**

| Name | Required | Type | Description |
|-------------------|----------|-------------------|--------------------------------------------------------------------|
| key | yes | String | The key against which to get the value |
| `MMKVStorage.API` | yes | `MMKVStorage.API` | MMKV storage instance created from `new MMKVLoader().initialize()` |
| defaultValue | no | String | Pass a default value for the hook if any |

**returns:** `{current: RefObject<T>, reset: () => void}`.

The `reset` function can be used to reset the value to `defaultValue`.

### `createMMKVRefHookForStorage`
A helper function that returns `useMMKVRef` which can then be used inside a component.
**Arguments**

| Name | Required | Type | Description |
|-------------------|----------|---------|--------------------------------------------------------------------|
| `MMKVStorage.API` | yes | boolean | MMKV storage instance created from `new MMKVLoader().initialize()` |

**returns:** `useMMKVRef(key:string, defaultValue:any)`

### How to use

Import `MMKVLoader` and `useMMKVRef` Hook.

```js
import { MMKVLoader, useMMKVRef } from "react-native-mmkv-storage";
```

Initialize the `MMKVLoader` instance.

```js
const MMKV = new MMKVLoader().initialize();
```

Next, in our component we are going to register our hook.

```jsx
const App = () => {
const name = useMMKVRef("username", MMKV);

return (
<View>
<TextInput
defaultValue={name.current}
onChangeText={(value) => {
name.current = value;
}}
/>
</View>
);
};
```
Now whenever you update `current` value, it will be stored in storage, until you call `reset` function;

Loading

0 comments on commit d03e59a

Please sign in to comment.