Skip to content

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

InputTypeDescription
pick(value: T) => PFunction to extract/derive the slice from main state
OutputTypeDescription
ReturnsSliceControl<P>Object with slice state management methods

Slice Control Methods

MethodDescription
valueCurrent slice value
use()React hook for the slice
watch()Watch slice changes
html()Render slice as HTML
WrapWrapper component for slice
HardWrapHard 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>
  );
}

Released under the MIT License.