Quick Start
Reflex is a lightweight state management library that lets you write simple and predictable code to manage state throughout your Roblox game. It is based on the Silo library and is inspired by Redux.
Installation
Reflex is available on npm and Wally:
- npm
- Yarn
- pnpm
- Wally
Terminal
npm install @rbxts/reflex
Terminal
yarn add @rbxts/reflex
Terminal
pnpm add @rbxts/reflex
wally.toml
[dependencies]
Reflex = "littensy/reflex@4.3.1"
Start using Reflex
You're now ready to use Reflex! Where Rodux uses stores, reducers, and actions, Reflex revolves around actions and producers. Create a producer with an initial state and a set of actions, and you're ready to go.
- TypeScript
- Luau
import { createProducer } from "@rbxts/reflex";
interface State {
count: number;
}
const initialState: State = {
count: 0,
};
const producer = createProducer(initialState, {
increment: (state) => ({ ...state, count: state.count + 1 }),
reset: (state) => ({ ...state, count: 0 }),
});
local Reflex = require(ReplicatedStorage.Packages.Reflex)
type Producer = Reflex.Producer<State, Actions>
type State = {
count: number,
}
type Actions = {
increment: () -> (),
reset: () -> (),
}
local initialState: State = {
count = 0,
}
local producer = Reflex.createProducer(initialState, {
increment = function(state: State): State
return { count = state.count + 1 }
end,
reset = function(): State
return { count = 0 }
end,
}) :: Producer
Use your producer anywhere
Reflex was designed to make managing your state simple and straightforward. Dispatch actions by calling the action directly, and read & subscribe to state with selectors.
- TypeScript
- Luau
producer.subscribe(
(state) => state.count,
(count) => {
print(`The count is now ${count}`);
},
);
producer.increment(); // The count is now 1
producer:subscribe(function(state)
return state.count
end, function(count)
print("The count is now " .. count)
end)
producer.increment() --> The count is now 1