All Projects β†’ alfredosalzillo β†’ flhooks

alfredosalzillo / flhooks

Licence: MIT license
React like Hooks implementation for Flutter.

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to flhooks

use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+71.05%)
Mutual labels:  hooks, custom-hook, react-hooks
Graphql Hooks
🎣 Minimal hooks-first GraphQL client
Stars: ✭ 1,610 (+4136.84%)
Mutual labels:  hooks, react-hooks
Awesome React Hooks
Awesome React Hooks
Stars: ✭ 7,616 (+19942.11%)
Mutual labels:  hooks, react-hooks
Fre
πŸ‘» Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+8307.89%)
Mutual labels:  hooks, react-hooks
Beautiful React Hooks
πŸ”₯ A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development πŸ”₯
Stars: ✭ 5,242 (+13694.74%)
Mutual labels:  hooks, react-hooks
The Platform
Web. Components. πŸ˜‚
Stars: ✭ 4,355 (+11360.53%)
Mutual labels:  hooks, react-hooks
React Form
βš›οΈ Hooks for managing form state and validation in React
Stars: ✭ 2,270 (+5873.68%)
Mutual labels:  hooks, react-hooks
React-Combine-Provider
combine react providers in ease
Stars: ✭ 29 (-23.68%)
Mutual labels:  hooks, react-hooks
use-async-resource
A custom React hook for simple data fetching with React Suspense
Stars: ✭ 92 (+142.11%)
Mutual labels:  custom-hook, react-hooks
react-class-hooks
React Hooks implementation for Class Components. Support React >= 15.3.2
Stars: ✭ 63 (+65.79%)
Mutual labels:  custom-hook, react-hooks
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-47.37%)
Mutual labels:  hooks, react-hooks
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+11807.89%)
Mutual labels:  hooks, react-hooks
React Hook Form
πŸ“‹ React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+65244.74%)
Mutual labels:  hooks, react-hooks
Formik
Build forms in React, without the tears 😭
Stars: ✭ 29,047 (+76339.47%)
Mutual labels:  hooks, react-hooks
Constate
React Context + State
Stars: ✭ 3,519 (+9160.53%)
Mutual labels:  hooks, react-hooks
Haunted
React's Hooks API implemented for web components πŸ‘»
Stars: ✭ 2,197 (+5681.58%)
Mutual labels:  hooks, react-hooks
useCustomHooks
πŸ“¦ npm package containing a set of custom hooks for your next React project.
Stars: ✭ 12 (-68.42%)
Mutual labels:  hooks, custom-hook
learn-react-typescript
Learning React contents with TypeScript (Hooks, Redux)
Stars: ✭ 15 (-60.53%)
Mutual labels:  hooks, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+42.11%)
Mutual labels:  hooks, react-hooks
flutter use
Play Flutter Hooks.
Stars: ✭ 150 (+294.74%)
Mutual labels:  react-hooks, flutter-hooks

Build Status codecov

flhooks

Write stateful functional Component in Flutter. React like Hooks implementation for Flutter.

This package is inspired by React Hooks.

Why Hooks

Like for React, Hooks try to be a simple method to share stateful logic between Component.

The goal of thi library is to devoid class extensions and mixin. Of course flutter is not designed for functional Component and Hooks.

Getting Started

You should ensure that you add the flhooks as a dependency in your flutter project.

dependencies:
 flhooks: "^1.1.0"

You should then run flutter packages upgrade or update your packages in IntelliJ.

Rules

When using Hooks, React Hooks rules must be followed.

Only Call Hooks at the Top Level

Don’t call Hooks inside loops, conditions, or nested functions. Hooks can only be used inside a HookBuilder builder param. They can also be used to create other hooks.

Simple Usage

Hooks can only be used inside the builder of an HookBuilder.

HookBuilder is like a StatefulBuilder how build the builder function. Hooks function can be used only in the builder function.

// Define a Slider Page
final SliderPage = () =>
    HookBuilder(
      builder: (BuildContext context) {
        // define a state of type double
        final example = useState(0.0);
        final onChanged = useCallback((double newValue) {
          // change example.value for update the value in state
          example.value = newValue;
        }, [example]);
        return Material(
          child: Center(
            child: Slider(
              key: sliderKey,
              value: example.value,
              onChanged: onChanged,
            ),
          ),
        );
      },
    );
// Start the app
void main() =>
    runApp(MaterialApp(
      home: SliderPage(),
    ));

Hooks

Currently implemented Hooks.

useMemo

useMemo return the memoized result of the call to fn.

fn will be recalled only if store change.

 final helloMessage = useMemo(() => 'Hello ${name}', [name]);

useCallback

useCallback return the first reference to fn.

fn reference will change only if store change.

final onClick = useCallback(() => showAwesomeMessage(input1, input2),
  [input1, input2]);

It's the same as passing () => fn to useMemo.

useState

useState return a StateController, HookState.value is the initial value passed to useState, or the last set using state.value = newValue.

state.value = newValue will trigger the rebuild of the StatefulBuilder.

final name = useState('');
// ... get the value
  Text(name.value);
//... update the value and rebuild the component
  onChange: (newValue) => name.value = newValue;

useEffect

useEffect exec fn at first call or if store change. If fn return a function, this will be called if store change or when the widget dispose.

useEffect(() {
  final pub = stream.listen(callback);
  return () => pub.cancel();
  }, [stream]);

useEffect is useful for handle async or stream subscription.

Custom Hooks

Custom Hooks function can be created composing other hooks function.

Custom Hooks name must start with 'use'.

V useAsync<V>(Future<V> Function() asyncFn, V initial, List store) {
  final state = useState(initial);
  useEffect(() {
    var active = true;
    asyncFn().then((result) {
      if (active) {
        state.value = result;
      }
    });
    return () {
      active = false;
    };
  }, store);
  return state.value;
}

Now you can use useAsync like any other hooks function.

Hot Reload

Hot reload is basically supported.

When the hock type change, because an hook function is added, removed, or change type, the hook will be disposed and reset to null.

However after an add or a remove, all hooks after the one how change, can be disposed or had a reset.

Pay attention, will be no break after hot reloading the app, but will be other side effects.

We decide to not make hooks shift to the next position, because we prefer to have the same behavior in the case you add, remove, or change an hook function call.

Feel free to open a issue or fork the repository to suggest a new implementation.

Example

More example in the example directory.

Changelog

Current version is 1.1.0, read the changelog for more info.

Next on flhooks

New hooks will be added in future like useFuture (or useAsync) and useStream, there will be no need to use FutureBuilder and StreamBuilder anymore.

We are actually testing some useIf conditional implementation of hooks.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].