React, but for Factorio. Packaged as a mod for easy consumption.
local React = require("__react__.react")
script.on_event(defines.events.on_player_created, function(event)
local player = game.players[event.player_index]
-- Create the root element we are going to put our GUI into (props can be customized)
local root = React.createRoot(player.gui.center, { style = "outer_frame" })
-- Build our virtual dom tree
local vdom = React.lsx[[
<frame caption="React for Factorio">
Hello, world!
<button>Click Me!</button>
</frame>
]]
-- Render!
React.render(vdom, root)
end)
Features
- Function components with hook support
useState
useReducer
useEffect
useMemo
useCallback
useRef
- Plain text components (generated via
label
) - Simple event API
- No need to register a separate event listener, just add a prop
- LSX, Lua version of JSX
lsx'<label caption="Hello world!" />'
is equivalent tocreateElement("label", { caption = "Hello world!" })
Coming Soon™
- Save/load event handler restoration
- Context API support (
createContext
/useContext
)
Credits
The core render loop/virtual dom is heavily inspired by O!, with other hook implementations referenced from Preact.