Stores
Manage global state
Sometimes you want to have a global state of your app, accross all your widgets. Although it is not recommended if you can use per-widget state management. In certain cases, like to know if the user is logged in or not, it becomes very useful.
Creating a Store
A Store is basically an object that contains state variables and functions to update it.
Create a new ModuleScript, and import the base Store class:
local Store = require(game.ReplicatedStorage.Common.Enoria.packages.store.Store)
local MyStore = function()
-- TODO
end
return MyStoreThe module is basically a function that will return our store object. We now need to create a new store object:
local Store = require(game.ReplicatedStorage.Common.Enoria.packages.store.Store)
local MyStore = function()
local store = Store.new({
LoggedIn = false
})
return store
end
return MyStoreThe Store constructor takes a dictionnary with state variables and their initial value. Then, you can add getters and setters:
Now, we have to create an action: something that will change a/multiple state variable(s) and rebuild the UI. It takes a dictonnary as parameter. In this example, we call a Remote Function to validate if the username and password are matching. If so, we call the setLoggedIn setter:
We can now import it and tell Enoria to use it in our main client script:
We are not finished yet! The store needs to know which widget(s) does he need to update on certain actions.
In a custom widget, access the store object and call the Listen method on an action:
This widget will now rebuild itself when login action is called. You can do that like so:
Last updated
Was this helpful?