Code Monkey home page Code Monkey logo

redux-reverb's Introduction

Example

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { applyMiddleware, compose, createStore } from 'redux';
import { Provider } from 'react-redux';
import promise from 'redux-promise-middleware';
import thunk from 'redux-thunk';
import sideEffectCreator from 'redux-reverb';
import rootSideEffect from 'sideEffects';
import reducers from 'state';
import App from './App';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const sideEffectMiddleware = sideEffectCreator(rootSideEffect);

const store = createStore(reducers, composeEnhancers(applyMiddleware(thunk, promise, sideEffectMiddleware)));

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root'),
);

sideEffects/index.js

import throttle from 'lodash/throttle';
import { SideEffect } from 'redux-reverb/lib/types';

const rootSideEffect: SideEffect = (action, previousState, currentState, dispatch) => {
  notificationSideEffect(action, previousState, currentState, dispatch);
  observeFirstName(action, previousState, currentState, dispatch);
};
export default rootSideEffect;

const notificationSideEffect: SideEffect = (action, previousState, currentState, dispatch) => {
  if (action.type === 'SET_FIRST_NAME_FULFILLED') dispatchNotification('First name successfully set', dispatch);
};

const dispatchNotification = (notificationText, dispatch) => {
  dispatch({
    type: 'PUSH_NOTIFICATION',
    payload: notificationText,
  });
  setTimeout(() => {
    dispatch({
      type: 'POP_NOTIFICATION',
    });
  }, 3000);
};

const throttledDispatchNotification = throttle((notificationText, dispatch) => {
  dispatch({
    type: 'PUSH_NOTIFICATION',
    payload: notificationText,
  });
  setTimeout(() => {
    dispatch({
      type: 'POP_NOTIFICATION',
    });
  }, 3000);
}, 2000);

const observeFirstName: SideEffect = (action, previousState, currentState, dispatch) => {
  if (previousState.user.firstName === null && currentState.user.firstName !== null) {
    console.log('user field was populated');
  }
};

redux-reverb's People

Contributors

conordavidson avatar

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.