Skip to content

HardWrap

Optimized wrapper component that prevents unnecessary parent re-renders.

Description

The HardWrap component is similar to Wrap but provides additional optimization by preventing parent component re-renders. It uses React.memo internally and is particularly useful when you want to isolate state updates to specific parts of your component tree.

Input/Output

InputTypeDescription
children(value: T) => React.JSX.ElementRender function that receives current state
OutputTypeDescription
ReturnsReact.ComponentMemoized React component that renders based on state

Example

typescript
import { signify } from 'react-signify';
import React from 'react';

const sExpensiveState = signify({ data: [], processing: false });
const sSimpleCounter = signify(0);

function ExpensiveParentComponent() {
  console.log('Parent component rendered'); // This should render less often
  
  return (
    <div>
      <h1>Expensive Component</h1>
      
      {/* Using HardWrap prevents parent re-renders */}
      <sExpensiveState.HardWrap>
        {(state) => (
          <div>
            <p>Processing: {state.processing ? 'Yes' : 'No'}</p>
            <p>Items: {state.data.length}</p>
            <button 
              onClick={() => sExpensiveState.set(({ value }) => {
                value.processing = !value.processing;
              })}
            >
              Toggle Processing
            </button>
          </div>
        )}
      </sExpensiveState.HardWrap>

      {/* Simple counter with HardWrap */}
      <sSimpleCounter.HardWrap>
        {(count) => (
          <div>
            <p>Counter: {count}</p>
            <button onClick={() => sSimpleCounter.set(count + 1)}>
              Increment
            </button>
          </div>
        )}
      </sSimpleCounter.HardWrap>
      
      <p>This text won't cause re-render when state changes above</p>
    </div>
  );
}

Released under the MIT License.