2.3.3 • Published 1 year ago

@firere/roact-hooked v2.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@firere/roact-hooked

Roact hooks based on Kampfkarren's hooks & React Hooks

Note

This fork exists purely for use with my version of Roact and is not compatible with @rbxts/roact.

Install

npm install @firere/roact-hooked

Usage with hook detection

Version 2.4.0 adds the withHookDetection function to allow hooks in function components without manually using HOCs.

It modifies Roact.createElement to detect hook use and wrap them in withHooks or withHooksPure HOCs.

// MyComponent.tsx
import Roact from "@firere/roact";
import { markPureComponent, useEffect, useState } from "@firere/roact-hooked";

interface Props {
	name?: string;
}

export default function MyComponent({ name = "David Baszucki" }: Props) {
	const [count, setCount] = useState(0);

	useEffect(() => {
		print("Counter: " + count);
	}, [count]);

	return (
		<textbutton
			Size={new UDim2(1, 0, 1, 0)}
			Text={`${name} pressed ${count} times`}
			Event={{
				Activated: () => setCount((value) => value + 1),
			}}
		/>
	);
}

// Optional: mark the component as a PureComponent
markPureComponent(MyComponent);
// mount.client.ts
import Roact from "@firere/roact";
import { withHookDetection } from "@firere/roact-hooked";
import { Players } from "@rbxts/services";
import MyComponent from "./MyComponent";

withHookDetection(Roact);

Roact.mount(<MyComponent />, Players.LocalPlayer.WaitForChild("PlayerGui"));

Usage with HOCs

Using the HOCs directly is still possible if you do not want to modify Roact.createElement for hook detection.

import Roact from "@firere/roact";
import { withHooks, withHooksPure, useEffect, useState } from "@firere/roact-hooked";

interface Props {
	name?: string;
}

function MyComponent({ name = "David Baszucki" }: Props) {
	const [count, setCount] = useState(0);

	useEffect(() => {
		print("Counter: " + count);
	}, [count]);

	return (
		<textbutton
			Size={new UDim2(1, 0, 1, 0)}
			Text={`${name} pressed ${count} times`}
			Event={{
				Activated: () => setCount((value) => value + 1),
			}}
		/>
	);
}

export default withHooks(MyComponent);

// Optional: wrap the FC in a PureComponent instead
export default withHooksPure(MyComponent);