Install in existing React/React Native app
Install in existing React/React Native app
Hana Store is automatically added to projects generated using
create-hana-app, but if you already have a React or React Native project, you can install and use it instantlyInstall from npmUsing your store
Hana store uses a very similar syntax to React’s built-inuseState() which allows you to read & write from the same hook. The useStore() hook takes in the name of the item in the store you want to use, and returns a setter/getter for it.
Store Initialization
Hana allows you to initialize your store with your own configuration which allows you to do things like setting an initial state, use state modules, sync to an external storage like localStorage or Async Storage, and so much moresrc/pages/_app.tsx
state key allows you to initialize your store with default values which will be returned on the initial load instead of null . This is especially useful when using TypeScript so you can prevent redundant null checks and avoid using ?.
You should only use
createStore()once in your application, and in a root component like the _app or similarSyncing to external storage
It’s very common to persist your state to an external or on-device storage, which allows you to maintain the application’s state even when the application is closed or device is restarted. This usually require’s annoying boilerplate or using an effect to watch states which run quite ineffeciently. Hana’s solution to this problem is a built-in store plugin that automatically syncs your global state to the browser’s localStorage or any other storage of your choice if specifiedPersistedState
- The
envproperty is used to tell the plugin that it is running in a React Native environment. This is important because React Native does not have awindowobject. If you do not pass this property, the plugin will assume that it is running in a browser environment. - The
storageproperty is used to tell the plugin which storage medium to use. This property can be any object that implements theStorageinterface. This includeslocalStorage,sessionStorage, andAsyncStoragefrom React Native. - It also supports a
keyproperty which is used to specify the key to use when saving the state. This defaults tohana-store.
- There is also an
excludeproperty is an array of keys that should not be persisted. This is useful for excluding sensitive data like passwords and tokens from being persisted. If a value is excluded, it will not be persisted, but it will still be available in the state object.
- The
includeproperty is an array of keys that should be persisted. This is useful for persisting only certain values in the state object. If theincludeproperty is set, theexcludeproperty will be ignored and only the values in theincludeproperty will be persisted.

