reduxjs / redux

A JS library for predictable global state management
https://redux.js.org
MIT License
60.88k stars 15.27k forks source link

Docs: Add redux-detector to the Ecosystem section #3557

Closed piotr-oles closed 5 years ago

piotr-oles commented 5 years ago

Hi everyone! I've created a library called redux-detector. Yesterday I've released a stable version 1.0.0 and I would be grateful if we could add this library to the Ecosystem section in the Redux documentation.

redux-detector is a library similar to other side-effect libraries like redux-saga or redux-observable. The difference is that redux-detector operates only on state transitions using pure functions. The most common usage is detecting if we enter a given route and then dispatching an action like fetching data. The example:

import { composeIf, composeOr, composeAnd, changedToTruthy, isTruthy } from 'redux-detector';
import { changedFilters } from 'store/filters/filtersDetector';
import { getFilters } from 'store/filters/filtersSelector';
import { changedPagination } from 'store/pagination/paginationDetector';
import { getPagination } from 'store/pagination/paginationSelector';
import { changedSorting } from 'store/sorting/sortingDetector';
import { getSorting } from 'store/sorting/sortingSelector';
import { hasMatch } from 'store/router/routerSelector';
import { listCompanies } from 'store/company/companyAction';
import { COMPANIES_ROUTE } from 'route/dashboard';

export const companyDetector = composeIf(
  composeOr(
    changedToTruthy(hasMatch(COMPANIES_ROUTE)),
    composeAnd(
      isTruthy(hasMatch(COMPANIES_ROUTE)),
      composeOr(
        changedPagination(getPagination),
        changedFilters(getFilters),
        changedSorting(getSorting)
      )
    )
  ),
  () => listCompanies()
);

The code above triggers listCompanies() action if we enter COMPANIES_ROUTE or when we already are on COMPANIES_ROUTE and pagination, filters or sorting has been changed. The listCompanies action is a thunk and looks like this:

export const LIST_COMPANIES = 'LIST_COMPANIES';
export const listCompanies = () => (dispatch, getState) => {
  const query = {
    pagination: getPagination(getState()),
    sorting: getSorting(getState()),
    filter: getFilters(getState())
  };

  return dispatch(async(LIST_COMPANIES, CompanyApi.list(query)));
};

If it sounds interesting for you I will create PR to the docs :)

markerikson commented 5 years ago

Hey. Looks neat, but we try to limit stuff in the "Ecosystem" page to libraries that have gained wider adoption or that we particularly recommend.

However, please feel free to submit a PR to add it to the large Redux Addons Catalog at https://github.com/markerikson/redux-ecosystem-links .