get()

The get() method allows you to retrieve the current state values synchronously from your store. You can access either the entire state object or select specific values using a selector function.

import { createStore } from 'finalstore';
 
const store = createStore({
  states: {
    count: 0,
    user: null as { name: string } | null
  },
  actions: {}, // Required, even if empty
  selectors: {
    userInfo: (state) => ({
      name: state.user?.name,
      count: state.count
    })
  }
});

Basic Usage

function handleClick() {
  // Get entire state
  const state = store.get();
  console.log('Current state:', state);
 
  // Get specific value using custom selector
  const count = store.get((state) => state.count);
  console.log('Current count:', count);
}

With Custom Selectors

function logUserData() {
  const userData = store.get((state) => ({
    name: state.user?.name,
    count: state.count
  }));
 
  console.log('User data:', userData);
}

With Predefined Selectors

function getUserInfo() {
  // Using predefined selector
  const userInfo = store.getSelector.userInfo();
  console.log('User info:', userInfo);
}

On this page