phaser-jsx
Version:
Use JSX in Phaser.
31 lines • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useState = useState;
var helpers_1 = require("../helpers");
/**
* A hook that lets you manage state for your game object.
*
* When the state is updated via the setter, it triggers a re-render
* of the associated game object.
*
* @param initialValue - The initial value for the state.
* @returns - An array with the current state value and a setter function.
*/
function useState(initialValue) {
var context = (0, helpers_1.getRenderContext)();
var key = context.getNextStateIndex();
// Initialize state if not already set
if (!context.state.has(key)) {
context.state.set(key, initialValue);
}
var value = context.state.get(key);
function setValue(newValue) {
var resolvedValue = typeof newValue === 'function'
? newValue(value)
: newValue;
context.state.set(key, resolvedValue);
context.rerender();
}
return [value, setValue];
}
//# sourceMappingURL=useState.js.map