slice
Method to create a derived state slice from the main state.
Description
The slice
method creates a new reactive state that is derived from a portion of the main state. The sliced state automatically updates when the relevant part of the main state changes, providing efficient subscriptions to specific data segments.
Input/Output
Input | Type | Description |
---|---|---|
pick | (value: T) => P | Function to extract/derive the slice from main state |
Output | Type | Description |
---|---|---|
Returns | SliceControl<P> | Object with slice state management methods |
Slice Control Methods
Method | Description |
---|---|
value | Current slice value |
use() | React hook for the slice |
watch() | Watch slice changes |
html() | Render slice as HTML |
Wrap | Wrapper component for slice |
HardWrap | Hard wrapper component for slice |
stop() | Stop slice rendering |
resume() | Resume slice rendering |
conditionRendering() | Set render condition for slice |
Example
typescript
import { signify } from 'react-signify';
import React from 'react';
const sUser = signify({
personal: { name: 'John', age: 25 },
settings: { theme: 'dark', notifications: true },
stats: { posts: 10, followers: 100 }
});
// Create slices
const ssPersonalInfo = sUser.slice(state => state.personal);
const ssUserStats = sUser.slice(state => state.stats);
function PersonalSection() {
const personal = ssPersonalInfo.use();
return (
<div>
<h2>{personal.name}</h2>
<p>Age: {personal.age}</p>
</div>
);
}
function StatsSection() {
const stats = ssUserStats.use();
return (
<div>
<p>Posts: {stats.posts}</p>
<p>Followers: {stats.followers}</p>
</div>
);
}