๐Ÿงธ
zfy
  • Getting started
  • ๐Ÿค“Guides
    • Creating & using a store
    • Persisting & rehydrating data
    • Using middlewares
    • Handling multiple stores
    • Type checking with TypeScript
  • ๐Ÿ“šAPI reference
    • Types
      • CreateStoreType
      • CreateStoreConfigType
      • CreateStoreOptionsType
      • StoreType
      • ZfyMiddlewareType
      • InitStoresType
      • InitStoresResetOptionsType
    • createStore
    • initStores
    • PersistGate
    • useRehydrate
  • ๐Ÿ—ƒ๏ธOthers
    • Help
    • Contributing
    • Changelog
    • GitHub
Powered by GitBook
On this page
  • ๐Ÿ—๏ธ Installation
  • ๐ŸŒบ Features
  • ๐Ÿ’ป Usage
  • ๐Ÿ’ซ Acknowledgments

Was this helpful?

Getting started

NextCreating & using a store

Last updated 5 months ago

Was this helpful?

is an amazing library that keeps state management as simple as you would have always loved it to be. zfy has been built on top of it to provide a useful set of tools to make that experience even more delightful.

๐Ÿ—๏ธ Installation

yarn add @colorfy-software/zfy
npm install @colorfy-software/zfy
npx expo install @colorfy-software/zfy

๐ŸŒบ Features

  • Fully typed with TypeScript

  • Standardized access/update API backed by Immer

  • Ability to manage & consume multiple stores at once

  • Simple API for store creation with custom middlewares

  • Out-of-the-box persist gate component & rehydration hook

  • Logger, persist & subscribe middlewares available via a simple flag

๐Ÿ’ป Usage

  1. Create your stores

import {
  PersistGate,
  createStore,
  initStores,
  useRehydrate,
} from '@colorfy-software/zfy'
import AsyncStorage from '@react-native-async-storage/async-storage'

const simpleStore = createStore('groceries', ['kimchi', 'bok choy', 'tteokbokki' ])
const persistedStore = createStore(
  'user',
  { firstName: 'Jolyne' },
  { persist: { getStorage: () => AsyncStorage } },
)
import { SafeAreaView, Text } from 'react-native'

const Loader = () => (
  <SafeAreaView>
    <Text>โณ Loading...</Text>
  </SafeAreaView>
)

const App = () => (
  <PersistGate stores={[persistedStore]} loader={<Loader />}>
    <MyApp />
  </PersistGate>
)

// or

const App = () => {
  const isRehydrated = useRehydrate([persistedStore])
  return isRehydrated ? <Loader /> : <MyApp />
}
const { stores, useStores } = initStores([simpleStore, persistedStore])
console.log(stores.simpleStore.getState().data[2]) // 'tteokbokki'

// ...or don't!

console.log(persistedStore.getState().data.firstName) // 'Jolyne'
const Component = () => {
  const itemToPurchase = useStores('groceries', data => data[0]) // 'kimchi'
  const userName = useStores('user', data => data.firstName) // 'Jolyne'  
  return null
}

// or

const Component = () => {
  const itemToPurchase = simpleStore('groceries', data => data[0]) // 'kimchi'
  const userName = persistedStore('user', data => data.firstName) // 'Jolyne'
  return null
}
stores.groceries.getState().update(data => {
  data[3] = 'chai'
})

// or

simpleStore.getState().update(data => {
  data[3] = 'chai'
})
stores.reset()

// ...or one by one!

simpleStore.getState().reset()
persistedStore.getState().reset()

๐Ÿ’ซ Acknowledgments

2. Rehydrate your data if needed

3. Bundle your stores as you please

4. Use the bundled/individual stores in your components

5. Update your data immutably (thanks to ) from wherever

6. Reset it all once your user is done

As you can imagine: major thanks to for such an amazing library! zfy also exists thanks to who made it so easy to deal with immutable state updates.

โ™ป๏ธ
๐Ÿ“ฆ
โš›๏ธ
๐Ÿงน
๐Ÿฃ
zustand
๐Ÿ†•
Immer
the team of contributors behind zustand
the folks working on Immer