flutter_redux_hooks 0.2.0 copy "flutter_redux_hooks: ^0.2.0" to clipboard
flutter_redux_hooks: ^0.2.0 copied to clipboard

A hook-based alternative to flutter_redux. Connect to your redux store in a familiar way.

flutter_redux_hooks #

pub package Build Status codecov

A set of utilities that allow you to easily consume a Redux Store to build Flutter Widgets.

This package is built to work with Redux.dart 5.0.0+.

This library is based on flutter_redux, it actually started as a fork of that project. The implementation of StoreProvider available here is the same you will find in that lib, I removed the rest of the widgets offered by it and replaced them with my hooks. Clearly, I aimed to replicate the behavior of the hooks you'll find in react-redux, if you think I succeeded let me know by leaving a star in the repo!

Redux Widgets #

  • StoreProvider - The base Widget. It will pass the given Redux Store to all descendants that request it.

Redux hooks #

  • useStore - Will return a reference to the store you provided via StoreProvider.
  • useDispatch - Will return a reference to the dispatch function for the store you provided via StoreProvider.
  • useSelector - Will return the result of applying a selector function to the state. To make these selectors I recommend either using reselect or redux_toolkit (redux_toolkit exports reselect).

Companion Libraries #

Usage #

Let's demo the basic usage with the all-time favorite: A counter example!

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart' show HookWidget;
import 'package:flutter_redux_hooks/flutter_redux_hooks.dart';
import 'package:redux/redux.dart';

enum Actions { Increment }

int counterReducer(int state, dynamic action) {
  if (action == Actions.Increment) {
    return state + 1;
  }

  return state;
}

void main() {
  final store = Store<int>(counterReducer, initialState: 0);

  runApp(
    StoreProvider<int>(
      store: store,
      child: FlutterReduxApp(
        title: 'Flutter Redux Demo',
      ),
    ),
  );
}

class FlutterReduxApp extends HookWidget {
  final String title;

  FlutterReduxApp({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final dispatch = useDispatch<int>();
    final count = useSelector<int, String>((state) => state.toString());

    return MaterialApp(
      theme: ThemeData.dark(),
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                count,
                style: TextStyle(color: Colors.white, fontSize: 36),
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => dispatch(Actions.Increment),
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

Contributors #

9
likes
150
pub points
70%
popularity

Publisher

unverified uploader

A hook-based alternative to flutter_redux. Connect to your redux store in a familiar way.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_hooks, redux

More

Packages that depend on flutter_redux_hooks