Slice
Info
Slices are central to managing your app's state. They bundle together actions, state fields, derived fields, and effects for cleaner and efficient state management.
To create a slice:
counter-slice.ts
import { createKey } from '@nalanda/core';
const key = createKey('<slice-name>', [
// dependencies
]);
export const yourSlice= key.slice({
// fields
// actions
});
Also read Key or Slice guide.
.name
The name of the slice.
Example:
slice.name // 'counter'
.get()
Gets the exposed field values in a given StoreState.
Signature:
slice.get(storeState: StoreState): { [name: fieldName]: value }
Arguments:
storeState
- The store state to get value from
.getField()
Gets the value of an exposed field.
Signature:
slice.getField(storeState: StoreState, fieldName: string): value
Arguments:
storeState
- The store state to get value fromfieldName
- The name of the exposed field to get value from
Example:
const slice = key.slice({
counter: counterField
})
slice.getField(storeState, 'counter') // 0
.track()
Tracks the value of exposed fields by returning an object.
This only works inside an effect.
Signature:
slice.track(store: EffectStore): ValueObject
Arguments:
⚠️
Tracking will only happen if you access the field names in the returned object. Read more about this in Effects guide.
.trackField()
Tracks the value of an exposed field.
This only works inside an effect.
Signature:
slice.trackField(store: EffectStore, fieldName: string): value
Arguments:
store
- the store passed by the effect callbackfieldName
- The name of the exposed field to track value from
Example:
const slice = key.slice({
counter: counterField
})
const counter = slice.trackField(storeState, 'counter');