flhooks 1.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 70

Build Status

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.

Getting Started #

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

dependencies:
 flhooks: "^0.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) {
          // call example.set for update the value in state
          example.set(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 an HookState, HookState.value is initial value passed to useState, or the last passed to HookState.set.

Will trigger the rebuild of the StatefulBuilder.

final name = useState('');
// ... get the value
  Text(name.value);
//... update the value
  onChange: (newValue) => name.set(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.set(result);
      }
    });
    return () {
      active = false;
    };
  }, store);
  return state.value;
}

Now you can use useAsync like any other hooks function.

Example #

More example in the example directory.

[1.0.0] - 26/12/2018

[0.0.2] - 25/12/2018

  • added useEffect hook function
  • better dartdoc comment
  • better readme
  • added travis integration
  • added test for useEffect
  • added test for hooks function scope

[0.0.1] - 23/12/2018

  • Initial Release

example/README.md

Flhooks example #

List of example app implemented using flhooks.

  • todo_app a simple todo app implemented using hooks.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flhooks: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flhooks/flhooks.dart';
  
Version Uploaded Documentation Archive
1.1.0 Jan 5, 2019 Go to the documentation of flhooks 1.1.0 Download flhooks 1.1.0 archive
1.0.1 Dec 25, 2018 Go to the documentation of flhooks 1.0.1 Download flhooks 1.0.1 archive
1.0.0 Dec 25, 2018 Go to the documentation of flhooks 1.0.0 Download flhooks 1.0.0 archive
0.0.2 Dec 24, 2018 Go to the documentation of flhooks 0.0.2 Download flhooks 0.0.2 archive
0.0.1 Dec 23, 2018 Go to the documentation of flhooks 0.0.1 Download flhooks 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on May 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.0
  • pana: 0.12.15
  • Flutter: 1.5.8

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/flhooks.dart.

Run flutter format to format lib/flhooks.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test