React for Factorio

by maxpowa

React-based GUI layer for Factorio mods. Inspired by my pain and suffering while trying to write clean, responsive GUI code.

Internal
4 months ago
1.1
11
Owner:
maxpowa
Source:
https://github.com/maxpowa/reactorio
Homepage:
https://github.com/maxpowa/reactorio
License:
MIT
Created:
4 months ago
Latest Version:
1.4.1 (4 months ago)
Factorio version:
1.1
Downloaded by:
11 users

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 to createElement("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.