Giter Site home page Giter Site logo

stephenleicht / autoproxy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ide/autoproxy

0.0 2.0 0.0 136 KB

A higher-order decorator to automatically proxy properties from the original function to the decorated one

License: MIT License

JavaScript 100.00%

autoproxy's Introduction

autoproxy

A higher-order decorator to automatically proxy properties from the original function to the decorated one so that higher-order components don't get in the way.

npm package

How does autoproxy help?

Autoproxy exposes a class' static and instance properties through a wrapper class.

For example, a React component with Redux looks like this:

@connect(data => { size: data.layout.size })
class Box extends React.Component {
  static NUMBER_OF_SIDES = 4;
  bounce() {
    ...
  }
  render() {
    ...
  }
}

This component defines a static property called NUMBER_OF_SIDES and method called bounce.

Without autoproxy

Without autoproxy, to access these properties we have to write code like this:

class Root extends React.Component {
  render() {
    return <Box ref={component => { this._box = component; }} />;
  }
  componentDidMount() {
    console.log('There are %d sides', Box.WrappedComponent.NUMBER_OF_SIDES);
    this._box.getWrappedInstance().bounce();
  }
}

See how we need WrappedComponent and getWrappedInstance()? This is because the connect decorator wraps the underlying class and Box refers to the wrapper class. As a result Box.NUMBER_OF_SIDES and this._box.bounce are both undefined.

With autoproxy

With autoproxy, the wrapper class automatically proxies the underlying class' properties. Use @autoproxy to decorate a decorator:

@autoproxy(connect(data => { size: data.layout.size }))
class Box extends React.Component {
  ...
}

Then you can write Box.NUMBER_OF_SIDES and this._box.bounce.

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.