BasicState is a really, really simple key-value based state management solution. It makes use of BindableEvents to allow your projects to watch for changes in state, and provides a simple API for communication with your state objects. Think Rodux, but much more simple.
Visit the documentation site to get started with BasicState, see examples, and view the full documentation.
A huge thanks to the contributors of this project. You've added some awesome new features and helps work out a few kinks.
Documentation is available on the documentation site.
For examples, please see the documentation site.
Basic example:
import BasicState from "@rbxts/BasicState";
interface IState {
Hello: String;
}
let State = new BasicState({
Hello: "World",
} as IState);
State.GetChangedSignal("Hello").Connect((NewValue, OldValue) => {
print(`Hello, ${NewValue}; goodbye ${OldValue}!`);
});
State.SetState({
Hello: "Roblox",
} as IState);
// Triggers the RBXScriptConnection above and prints
// "Hello, Roblox; goodbye World!"
Usage with Roact (EXAMPLE NOT IN TYPESCRIPT)
MyProject.Store.lua
:
local BasicState = require(path.to.BasicState)
local Store = BasicState.new({
Hello = "World"
})
return Store
MyProject.Components.MyComponent.lua
:
local Roact = require(path.to.Roact)
local MyComponent = Roact.Component:extend("MyComponent")
local Store = require(script.Parent.Parent.Store)
function MyComponent:render()
return Roact.createElement("TextButton", {
Text = string.format("Hello, %s!", self.state.Hello),
--> Displays "Hello, World!"
[Roact.Event.MouseButton1Click] = function()
Store:SetState({ Hello = "Roblox" })
--> Will re-render and display "Hello, Roblox!"
end
})
end
--[[
Wrap the component with the BasicState store and inject
the value of Hello into the component state.
--]]
return Store:Roact(MyComponent, { "Hello" })
Please refer to the thread on the Roblox Developer Forums if you wish to discuss BasicState. You can also contact me via direct message on the DevForums.