Giter VIP home page Giter VIP logo

redux-feature-data's Introduction

Commitizen friendly

Redux Feature Data

Redux Feature Data is a generic set of actions, sagas and selectors to reduce the boilerplate required for interacting with API's in a react project that uses redux-sagas as their middleware.

Getting Started

Add the redux feature data reducer to your reducers:

If you're using reduxjs toolkit, it should look something like this:

import { createFeatureDataReducer } from "redux-feature-data";
import { schema } from "normalizr";

// need to pass the normalizr entities so that redux-feature-data
// understands the relationship between entities
const users = new schema.Entity("users");
const featureDataReducer = createFeatureDataReducer({ users });

export default configureStore({
  reducer: {
    // other reducers here
    featureData: featureDataReducer, // The name of this slice can be whatever you want
  },
});

If you aren't using reduxjs toolkit, it should look something like this:

import { combineReducers } from "redux";
import { schema } from "normalizr";

const users = new schema.Entity("users");

const featureDataReducer = createFeatureDataReducer({ users });

export default combineReducers({
  // other reducers here
  featureData: featureDataReducer,
});

Add the redux feature data saga to your sagas:

If you're using reduxjs toolkit, it should look something like this:

import { configureStore } from "@reduxjs/toolkit";
import createSagaMiddleware from "redux-saga";
import { createFeatureDataReducer, featureSagasRoot } from "redux-feature-data";
import { schema } from "normalizr";

const users = new schema.Entity("users");
const sagaMiddleware = createSagaMiddleware();
const featureDataReducer = createFeatureDataReducer({ users });

export default configureStore({
  reducer: {
    // other reducers here
    featureData: featureDataReducer,
  },
  middleware: [sagaMiddleware],
});

sagaMiddleware.run(featureSagasRoot);

Now you're free to use the actions and selectors in your components

import React, { useEffect } from "react";
import axios from "axios";
import { useSelector, useDispatch } from "react-redux";
import { featureDataActions, featureSelectors } from "redux-feature-data";

const {
  makeGetDenormalizedData,
  makeGetHasFeatureFetchedSuccess,
} = featureSelectors;

export function Counter() {
  const dispatch = useDispatch();
  const getDenormalizedData = makeGetDenormalizedData({
    name: "counter",
    entity: "users",
  });
  const getHasFeatureFetchedSuccess = makeGetHasFeatureFetchedSuccess({
    name: "counter",
  });
  const hasUsersFetched = useSelector(getHasFeatureFetchedSuccess);
  const data = useSelector(getDenormalizedData);

  useEffect(() => {
    console.log(data);
  }, [hasUsersFetched]);

  return (
    <div>
      {hasUsersFetched && (
        <ul>
          {data.users?.map((user) => (
            <li key={user.id}>{user.name}</li>
          ))}
        </ul>
      )}
      <button
        onClick={() => {
          dispatch(
            featureDataActions.onFetchData({
              name: "counter",
              entity: "users",
              callback: () => axios.get("https://api.mocki.io/v1/f8750ac9"),
            })
          );
        }}
      >
        Add Async
      </button>
    </div>
  );
}

If you're curious about the pattern being used for the selector here to pass arguments, feel free to read the reselect documentation for an in-depth answer to this question.

Demo

If you'd like to see redux-feature-data in action, feel free to use this codesandbox.io link to try it out without having to write any code.

API

Link to api docs

Contributing

To contribute, please fork this repository and then make sure to use yarn commit to create commits.

To test your code locally in another project, you can run yarn link in this repo, and then yarn link redux-feature-data in the project where you have run yarn install redux-feature-data.

redux-feature-data's People

Contributors

gkadillak avatar gkadillak-lyra avatar juddruckers avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

juddruckers

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.