Giter Site home page Giter Site logo

roact-reflex's Introduction

Reflex
Reflex for Roact

GitHub Workflow Status npm version npm downloads GitHub license


 

♻️ Reflex

Reflex is a simple state container inspired by Rodux and Silo, designed to be an all-in-one solution for managing and reacting to state in Roblox games.

This package provides Reflex bindings for Roact using @rbxts/roact-hooked.

See the full documentation for Reflex here.

Important

This repository has been superseded by littensy/react-reflex.

 

📦 Installation

$ npm install @rbxts/roact-reflex
$ pnpm add @rbxts/roact-reflex

 

📚 Usage

Reflex offers support for @rbxts/roact-hooked with @rbxts/roact-reflex. Using roact-reflex hooks requires setting up a ReflexProvider at the root of your Roact tree.

If you don't want to use generics to get the Producer type you want, Reflex exports the UseSelectorHook and UseProducerHook types to make it easier:

export const useRootProducer: UseProducerHook<RootProducer> = useProducer;
export const useRootSelector: UseSelectorHook<RootProducer> = useSelector;
export function App() {
	const { increment, decrement } = useRootProducer();

	const count = useRootSelector((state) => state.count);

	return (
		<textbutton
			Text={`Count: ${count}`}
			AnchorPoint={new Vector2(0.5, 0.5)}
			Size={new UDim2(0, 100, 0, 50)}
			Position={new UDim2(0.5, 0, 0.5, 0)}
			Event={{
				Activated: () => increment(),
				MouseButton2Click: () => decrement(),
			}}
		/>
	);
}
Roact.mount(
	<ReflexProvider producer={producer}>
		<App />
	</ReflexProvider>,
);

When using selector creators, you should avoid creating them in your render method (i.e. useSelector(selectPlayersOnTeam(redTeam))), since it creates a new selector every time the component renders and risks excessive re-renders. Instead, you can use the useSelectorCreator() hook to memoize the selector:

export const selectPlayersOnTeam = (team: Team) => {
	return createSelector([selectPlayers] as const, (players) => {
		return players.filter((player) => player.Team === team);
	});
};
const players = useSelectorCreator(selectPlayersOnTeam, redTeam);

 

📝 License

Reflex is licensed under the MIT License.

roact-reflex's People

Contributors

littensy avatar

Watchers

 avatar

Forkers

hy2k

roact-reflex's Issues

Throw more descriptive errors

Roact Reflex should throw more descriptive errors for oversights like:

  • Selectors returning different values for the same state argument
  • Using useSelector or useSelectorCreator without a root <ReflexProvider>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.