Nalanda
State Management
Effortlessly Powerful State Management
Simple to Start, Designed to Scale
Run side-effects in a predictable and easy to understand manner.
key.effect(store => {
// automatically runs the effect when isLoggedIn changes
const { isLoggedIn } = loginSlice.track(store);
if (isLoggedIn) {
alert('Welcome back');
} else {
alert('Please login');
}
});
Confidently derive data with efficient lazy evaluation and memoization.
const totalPriceField = key.derive(state => {
// derive `totalPriceField` from the `cartItems`
const { cartItems} = cartSlice.get(state);
return reallyExpensiveCalculation(cartItems);
});
// reading the value is lazy and memoized
totalPriceField.get(store.state);
Nalanda empowers you with state encapsulation, ensuring clear boundaries between different parts of your application.
// keep certain state internal only
const internalItemIndex = key.field(0);
// use the internal field to derive the itemIndex which is exposed
const itemIndex = key.derive(state => {
const index = internalItemIndex.get(state);
return index < 0 ? 0 : internalCounter;
});
// only expose itemIndex to the outside world using Slice
export const itemSlice = key.slice({
itemIndex,
});
React bindings ensure automatic and efficient re-rendering of your components.
import { itemSlice } from './item-slice';
function UserGreeting() {
const { itemIndex } = useTrack(itemSlice)
// ^ automatically re-renders whenever itemIndex changes
return (
<div>
The item index is {itemIndex}
</div>
)
}