Last active
September 29, 2024 00:59
-
-
Save sasial-dev/751733e0e97d91d3cca064a75c792d68 to your computer and use it in GitHub Desktop.
React + Matter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Under the MIT | |
import { useHookState } from "@rbxts/matter"; | |
import { createElement as e } from "@rbxts/react"; | |
import type { Root } from "@rbxts/react-roblox"; | |
import { mount } from "shared/gui"; | |
interface HandleState { | |
props: object; | |
handle?: Root; | |
} | |
export = <P extends object>( | |
element: React.FunctionComponent<P> | React.ComponentClass<P> | string, | |
props: P, | |
parent: Instance, | |
...children: React.ReactNode[] | |
) => { | |
const state = useHookState<HandleState>(parent, (state) => state.handle?.unmount()); | |
const sameProps = props === state.props; | |
state.props = props; | |
if (state.handle === undefined) { | |
state.handle = mount(e(element, props, ...children), parent); | |
} else if (!sameProps) { | |
state.handle.render(e(element, props, ...children)); | |
} | |
return state.handle; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the
mount
function: