Git Product home page Git Product logo

ariana's Introduction

Ariana : React helper to ease passing parent components callbacks down to their children.

  • Ariana ease passing components callbacks down to their children in a react application.

Installation

npm install --save ariana

Usage

import Ariana from 'ariana';
  1. Create a 'callbacks object' (an object that contains all component's callbacks) :
//In Main.jsx
const mainCallbacks = {
  handleFiltering: filterInput => this.setState(filterInput),
  log: s => alert(s)
};
  1. Instantiate a new root wrapper with the callbacks object by calling Ariana :
//In Main.jsx
const wrapper = Ariana(mainCallbacks);
  1. Pass the 'wrapper' to children components using 'props' :
//In Main.jsx
<Filters
   text={this.state.filterText}
   stockOnly={this.state.inStockOnly}
   ariana={wrapper}
   />
  1. Use any callback in a child component using the 'wrapper' passed via 'props' :
//In Filters.jsx
const { ariana } = this.props;

ariana.handleFiltering({
  filterText: "Ariana"
});
  1. Overload parent callbacks behaviour by adding new callbacks functions at each stage of the react-app's components tree:
//In Table/index.jsx
const { ariana } = this.props;

const tableCallbacks = {
  handleSorting: newSort => this.setState(newSort),
  log: () =>
    ariana.log(
      "Wow... Table/index.jsx component's 'log' callback has been called."
    ) //overload parent's 'log' callback
};

//add a child wrapper to handle callbacks of this child component
const lisa = ariana.child(tableCallbacks);

//pass lisa(ariana's child) via props to this component's child (Header.jsx)
<Header lisa={lisa} />
  1. Access a grandparent's callbacks by using the wrapper's 'parent' property :
//In Header.jsx
<button
  onClick={() => {
    /*use the the parent component's callback*/
    lisa.handleSorting({
      sort: {
        by: column,
        asc: !true
      }
    });
    /*use the the grandparent component's callback*/
    lisa.parent.log(
      "Jeez... Main.jsx component's 'log' callback has been called."
    ); //it could be possible to do lisa.parent.parent.[...].log()
  }}
>
  1. At any time you can create a copy of the callbacks object contained in the wrapper :
const mainCallbacksCopy = ariana.eject();

Limitation

  • You can use any key name in callback objects except the words reserved by Ariana : 'child', 'parent', 'val' and 'eject' which are functions names of the wrapper.

Example

See the full example here.

ariana's People

Contributors

tutanck avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.