Get started with seamless-immutable CDN

BSD-3-Clause licensed

Immutable data structures with seamless API . Efficient, predictable updates without deep cloning.

Tags:
  • immutable

Stable version

Copied!

How to start using seamless-immutable CDN


// Include Seamless Immutable library
const IM = window.SeamlessImmutable;

// Create an initial immutable state
const initialState = IM.Map({
  count: IM.Int(0),
  name: IM.String('John Doe'),
});

// Create a reducer function
const reducer = (state, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state.set('count', state.get('count').increment(1));
    case 'SET_NAME':
      return state.set('name', IM.String(action.payload));
    default:
      throw new Error();
  }
};

// Create a store using the reducer and initial state
const store = IM.State(reducer, initialState);

// Dispatch actions to update the state
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'SET_NAME', payload: 'Jane Doe' });

// Get the current state
const currentState = store.getState();

// Update the state directly (not recommended)
store.set('count', store.get('count').increment(1));

console.log(currentState.toJS()); // { count: 1, name: 'Jane Doe' }
Copied!
Copied!
Copied!

All versions