2.6.2 • Published 6 months ago

@rbxts/roact-hooked v2.6.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@rbxts/roact-hooked

Roact hooks based on Kampfkarren's hooks & React Hooks

Install

npm install @rbxts/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 "@rbxts/roact";
import { markPureComponent, useEffect, useState } from "@rbxts/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 "@rbxts/roact";
import { withHookDetection } from "@rbxts/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 "@rbxts/roact";
import { withHooks, withHooksPure, useEffect, useState } from "@rbxts/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);
2.6.1

8 months ago

2.6.2

6 months ago

2.5.0

11 months ago

2.4.0

1 year ago

2.6.0

11 months ago

2.5.1

11 months ago

2.3.0

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.3

1 year ago

2.2.0

1 year ago

2.1.3

1 year ago

2.1.2

2 years ago

2.1.1

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago