Skip to content

Commit

Permalink
Merge pull request #10 from kuasha420/docs/readme-update
Browse files Browse the repository at this point in the history
Update readme file to add better example and explain the default storage import
  • Loading branch information
kuasha420 authored Oct 14, 2024
2 parents 4844738 + 89ea410 commit 3f020cf
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,21 @@ Below is an example on how to create the provider and consumer hook.
// store-setup.ts
import { types } from 'mobx-state-tree';
import createPersistentStore from 'mst-persistent-store';
// The default storage is async-storage for React Native and localforage for Web
// This needs to be explicitly imported for the default storage to work correctly
// for the respective platform. If you are using a custom storage,
// you don't need to import this
import defaultStorage from 'mst-persistent-store/dist/storage';

const PersistentStore = types
.model('RootStore', {
name: types.string,
age: types.number,
premium: types.boolean,
hydrated: types.boolean,
})
.actions((self) => ({
hydrate() {
self.hydrated = true;
grantPremium() {
self.premium = true;
},
}))
.views((self) => ({
Expand All @@ -73,14 +76,6 @@ export const [PersistentStoreProvider, usePersistentStore] = createPersistentSto
name: 'Test User',
age: 19,
premium: false,
hydrated: false,
},
{
hydrated: false,
},
{
logging: false,
devtool: false,
}
);
```
Expand Down Expand Up @@ -114,20 +109,12 @@ import { useEffect } from 'react';
import { usePersistentStore } from './store-setup';

const Main = observer(() => {
const { name, age, isAdult, hydrated, hydrate } = usePersistentStore();

useEffect(() => {
hydrate();
}, []);

if (!hydrated) {
return <p>Loading...</p>;
}
const store = usePersistentStore();

return (
<div>
<p>
{name} is {age} years old and {isAdult ? 'is' : 'is not'} an adult.
{store.name} is {store.age} years old and {store.isAdult ? 'is' : 'is not'} an adult.
</p>
</div>
);
Expand Down Expand Up @@ -172,13 +159,6 @@ export const [PersistentStoreProvider, usePersistentStore] = createPersistentSto
name: 'Test User',
age: 19,
premium: false,
hydrated: false,
},
{
hydrated: false,
},
{
hydrated: false,
}
);
```
Expand Down

0 comments on commit 3f020cf

Please sign in to comment.