Coder Social home page Coder Social logo

mcanthony / elegant-react-hot-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gilbox/elegant-react-hot-demo

1.0 2.0 0.0 1.03 MB

demo with hot reload and time travel, tracked derived data, react-motion, and stream-based plugins

License: MIT License

JavaScript 99.98% HTML 0.02%

elegant-react-hot-demo's Introduction

elegant-react-hot-demo

elegant-react-based demo with FRP stream plugins (using flyd), time travel, and hot updates using just webpack HMR (without react-hot-loader).

Live Demo

What are stream plugins?

Similar to Flux stores, stream plugins manage application state. And similar to Redux stores, stream plugins generally don't have their own state, although for the sake of simplicity the time-travel plugin keeps the history stored locally.

Unlike Flux stores, stream plugins are highly modular. They feature a single point of connection to your application, ie the plugin function. The arguments to the plugin function are any number of read-only and write-only streams. So plugins are also highly encapsulated as well, they only have access to read and write exactly what you give them access to.

counter-plugin.js

This demo application uses a plugin called counter-plugin. In a real life app this might be a poor use-case for a plugin. Instead, you could just call a (sub)edit function from the App component.

Plugins should probably only be used when you really need some state-transform logic encapsulated outside of your component tree.

@derive and @track

@derive is an ES7 decorator that provides a nice declarative way to create a HoC that creates new props for it's child (decorated) component, which is data derived from it's props. @derive's single argument is an object with functions that transform props.

When used in conjunction with @track to specify which props will cause a derived method to require re-calculation, @derive will automatically optimize to only recalculate as necessary.

// because of @track, when only fontSize prop changes,
// 'greeting' prop won't be recalculated
@derive({
  @track('name')
  greeting({name}) {
    return `Hello ${name}!!`;
  }
})
class Hello extends Component {
  render() {
    const {greeting,fontSize} = this.props;
    return <h2 style={{fontSize}}>{greeting}</h2>;
  }
}

The Hello component can be used like so:

<Hello name="Benji" fontSize={Math.random() * 10 + 10} />

Note that a @derive transform function can be named the same as any prop. For example, the function greeting could have been called name although it doesn't really make sense in this context.

Usage

npm install
npm start
open http://localhost:3000

Dependencies

elegant-react-hot-demo's People

Contributors

alexkuz avatar gaearon avatar gilbox avatar hulufei avatar lancefisher avatar sd0s avatar werme avatar

Stargazers

 avatar

Watchers

 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.