Git Product home page Git Product logo

redux-fly's Introduction

Redux-fly

The library is focused on to providing simple API for:

  • Reducers registration at any time to any place in store of Redux.
  • Simple creation and mutation of component state, similar to local state, which is stored in store of Redux.
  • Creation of reused components which store own state in store of Redux.

Build Status

Example of container component creation which stores the state in store of Redux.

// Root component
import React from 'react';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { enhanceStore } from 'redux-fly';
import Counter from './Counter';

const store = createStore(() => {}, enhanceStore);

export default () => (
  <Provider store={store}>
    <Counter/>
  </Provider>
);

// Counter component
import React from 'react';
import { createReducer } from 'redux-fly';

const Counter = ({ reduxState: { counter }, reduxSetState }) => (
  <div>
    Clicked: {counter} times
    {' '}
    <button onClick={() => reduxSetState('INCREMENT', state => ({ counter: state.counter + 1 }))}>
      +
    </button>
  </div>
);

export default createReducer({
  mountPath: 'ui counter', // Reducer mounting path
  initialState: {
    counter: 0
  }
})(Counter);

Example of creation reused modal window component which stores the state in store of Redux.

// Modal window component
import React from 'react';
import { createReducer, getState } from 'redux-fly';
import { MENU_OPEN } from './Menu'; // Action of other component

const Modal = ({ reduxState: { opened }, children, reduxSetState }) => (
  <div style={{ display: opened ? 'block' : 'none' }}>
    <a onClick={() => reduxSetState('PRIVATE-CLOSE-MODAL', { opened: false })}>&times;</a>
    {children}
  </div>
);

// Type of window closing action (other components might listen in reducers)
export const actionPrivateCloseModal = (actionPrefix) => `${actionPrefix}/@PRIVATE-CLOSE-MODAL`;

// To open a modal is public action creator (other components might control the state)
export const createActionOpenModal = (actionPrefix) => ({ type: `${actionPrefix}/PUBLIC-OPEN-MODAL` });

// To close a modal is public action creator (other components might control the state)
export const createActionCloseModal = (actionPrefix) => ({ type: `${actionPrefix}/PUBLIC-CLOSE-MODAL` });

// Check is opened modal (other components might check the state)
export const isOpened = (mountPath, allState) => {
  const state = getState(mountPath)(allState)
  if (state) {
    return state.opened
  } else {
    throw new Error(`Mounting path ${mountPath} isn't valid`)
  }
}

export default createReducer({
  initialState: ({
    opened: false
  }),
  listenActions: (state, action, props, actionPrefix) => { // Listen public actions
    switch (action.type) {
      case createActionOpenModal(actionPrefix).type: // Listen own action
        return { ...state, opened: true };

      case MENU_OPEN: // Listen action of other component
      case createActionCloseModal(actionPrefix).type: // Listen own action
        return { ...state, opened: false };

      default:
        return state;
    }
  }
})(Modal);

Example of mounting canonical reducer.

import React from 'react';
import { registerReducers } from 'redux-fly';
import { connect } from 'react-redux';
import { compose } from 'redux';
import reducer, { * as actionCreators } from './reducer';

const Counter = ({ counter, increment }) => (
  <div>
    Clicked: {counter} times
    {' '}
    <button onClick={() => increment()}>
      +
    </button>
  </div>
);

export default compose(
  registerReducers({
    'ui counter': reducer
  }),
  connect((state) => ({ counter: state.ui.counter.value }), actionCreators)
)(Counter);

Is more information and examples below.

Installation

React-fly requires React 15.x, Redux 3.x and React Redux 4.x.

npm install --save redux-fly

This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.

If you don’t yet use npm or a modern module bundler, and would rather prefer a single-file UMD build that makes ReduxFly available as a global object, you can grab a pre-built versions: full and minified.

Documentation

Examples (view)

All examples use redux-devtools-extension if it is installed in browser.

  • Counter. Example to use redux-fly component state.
  • Async. Example to use of mix canonical reducer and redux-fly component state.
  • Universal. Example to use redux-fly for creation of component state and showin how to implement the universal rendering.
  • Reused components. Example to use redux-fly for creation reused components and showin how to implement the interaction between components.
  • Nested reused components. Example to use redux-fly for creation nested reused in reused components.
  • RandomGif. Example is based on the famous RandomGif (JS / Elm example that is often used to showcase Elm architecture).

redux-fly's People

Contributors

mrefrem avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

shadowwzw

redux-fly's Issues

any downside?

Hi,

I see this project was created little while ago and want to know any downside you have noticed during expansion of the large project after using it this long.

Library use cases and purpose?

Hi. You've got a bunch of interesting code going on here. Skimming through it, it looks like you're implementing something along the lines of encapsulated components and Redux reducer logic. Any chance you could put up a description of what the library is intended to do and how to use it?

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.