Git Product home page Git Product logo

meteor-react-utils's Introduction

React Meteor Utils

This package contains a React Mixin and some utilies for using React with Meteor.

Getting Started

meteor add ccorcos:react
meteor add ccorcos:react-utils

API

React.MeteorMixin

React.MeteorMixin allows you to tightly integrate Meteor with React.

This mixin with convert any props into reactive variables this.rprops so you can use tem with getMeteorState for fine-grained reactivity of the state. If a props is an instance of ReactiveVar, then it will be passed into rprops as you might expect.

getMeteorState: {
  post() {
    return Posts.findOne(this.rprops.postId.get())
  }
}

Another convenience of this mixin is this.linkVar which is similar to this.linkState except it links an input to a ReactiveVar. This allows you to pass a reactive var as props to multiple components and both could change and reactively update to that ReactiveVar without having to handle all the piping in their common ancestor.

React.createClassFactory
  displayName: 'Header'
  mixins: [React.MeteorMixin, React.addons.PureRenderMixin]
  propTypes:
    searchVar: React.PropTypes.instanceOf(ReactiveVar).isRequired
  render: ->
    (div {},
      (input {type:'text', placeholder:'search', valueLink:@linkVar(@props.searchVar)})
    )

React.createClassFactory
  displayName: 'Results'
  mixins: [React.MeteorMixin, React.addons.PureRenderMixin]
  propTypes:
    searchVar: React.PropTypes.instanceOf(ReactiveVar).isRequired
  getMeteorState:
    postIds: -> 
      filter = new RegExp(@rprops.searchVar.get(), 'ig')
      posts = Posts.find({title: filter}, {sort: {name:1, date:-1}, fields: {_id: 1}}).fetch()
      _.pluck posts, '_id'
  render: ->  
    (div {},
      @state.postIds.map (postId) ->
        (Post {key: postId, postId: postId})
    )

React.createClassFactory
  displayName: 'Search'
  getInitialState: ->
    searchVar: new ReactiveVar('')
  render: ->
    (div {},
      (Header {searchVar: @state.searchVar})
      (Results {searchVar: @state.searchVar})
    )

Another helpful function is this.sessionVar which creates a ReactiveVar that binds to a Session variable. This way, the state of your app will persist across hot-code pushes and the state of your components will be maintained between mounts and unmounts.

React.createClassFactory
  displayName: 'Search'
  mixins: [React.MeteorMixin, React.addons.PureRenderMixin]
  getMeteorState:
    searchVar: -> @sessionVar('searchText')
  render: ->
    # ...

This mixin has getMeteorSubs which runs your subscriptions within an autorun so they will be automatically stopped once componentWillUnmount is called.

React.createClassFactory

React.createClassFactory creates a class as you could expect, but also uses the display name to add the class to a global React.classes object and uses React.createFactory to add a factory of that class to a global React.factories object. This way, you can conveniently access you different classes from different files without polluting your global namespace. React.createClassFactory returns an array where the first element is the class and the second element is the factory of that class.

Factories are convenient if you don't want to use JSX and saves you from using React.createElement everywhere, especially for coffeescript.

React.renderBody

React.renderBody is a simple wrapper that renders to the body of the document. This works nicely with meteorhacks:flow-router.

FlowRouter.route('/', 
  action(params, queryParams) {
    Main = React.classes.Main
    React.renderBody(<Main/>)
});
FlowRouter.route '/', 
  action: (params, queryParams) ->
    Main = React.factories.Main
    React.renderBody Main()

To Do

  • subscribe example
  • waitOn
  • fastRender (SSR)

meteor-react-utils's People

Contributors

ccorcos avatar

Watchers

James Cloos 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.