Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Accessing your state

Michael Darko edited this page Apr 9, 2022 · 1 revision

There are 2 ways to access your global state with GlassX.

useStore

We've already seen how to set your state using useStore, if you've worked with React's useState, then you should have absolutely no issues working with this method. useStore gives you a simple way to reactively get your state and listen for updates. This means that when your state value changes, GlassX will smartly update your component using that paticular state value in view.

To get started, call useStore with the value you want to get from your global store:

import { useStore } from 'glassx';

const [something, setSomething] = useStore<SomeType>('someState');

// `something` holds the current value set for `someState`
<div>{something}</div>;

GlassX.get

As the name implies, this method allows you to directly pull up a value from your global store. All you need to do is pass in the key of the value you want to get. One thing to note is that the value gotten from GlassX.get is NOT reactive. This means that when the state changes, there will be no re-renders.

Note that this changes when there is the presence of useStore accessing the same value in that same component.

import GlassX from 'glasssx';

const item = GlassX.get('someState');
Clone this wiki locally