npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

coffee-react-class

v0.1.1

Published

React component as a coffee-script class (autobind, mixins, ...)

Downloads

5

Readme

Coffeescript React Class npm version

React component as a coffee-script class (autobind, mixins, ...)

Installation

npm install coffee-react-class

To see how to use Coffeescript class with React, please check this post

Usage

This package provides two elements: Component and mixins. They can be used together or separately.

Component

Component extends React's Component built-in class to provide auto-binding.

React.createClass has a built-in magic feature that bound all methods to this automatically for you. This can be a little confusing for JavaScript developers that are not used to this feature in other classes, or it can be confusing when they move from React to other classes.

Therefore we decided not to have this built-in into React's class model. You can still explicitly prebind methods in your constructor if you want.

React = require 'react'

class ButtonComponent extends React.Component

  constructor: (props) ->
    super props

    # comes from fat arrow.
    # @onClick = @onClick.bind this

  # notice the fat arrow here.
  # render shouldn't have fat arrow etc.
  # it's just confusing.
  onClick: => @setState clicked: yes

  render: -> <button onClick={@onClick}>My button</button>
# Note, I left React module because the JSX tags are transformed
# to `React.createElement` so we still need to import this module
React = require 'react'
{ Component } = require 'coffee-react-class'

# Extending `Component` instead of `React.Component`
class MyButton extends Component {
  constructor: (props) ->
    super props
    # Use `super(props, false);` to not autobind
    # Or `this.bind(['onClick']);` to bind only some methods

  # no fat arrow, or explicit binding.
  # Automatically bind to class instance
  onClick: -> @setState clicked: yes

  render: -> <button onClick={this.onClick}>My button</button>
}

Mixins

Mixins provides compatibility with React.createClass mixins. Original idea from react-mixin.

Unfortunately, we will not launch any mixin support for ES6 classes in React. That would defeat the purpose of only using idiomatic JavaScript concepts.

There is no standard and universal way to define mixins in JavaScript. In fact, several features to support mixins were dropped from ES6 today. There are a lot of libraries with different semantics. We think that there should be one way of defining mixins that you can use for any JavaScript class. React just making another doesn't help that effort.

But if you still want to use mixins with Coffeescript classes. See below how.

  • componentClass Component factory (not class instance).
  • mixins Array of mixin objects.
  • options
    • defaultRule Default rule to apply to property not defined in rules
    • rules Map mixin properties to rules
{
  rules: {
    # Lifecycle methods
    componentWillMount:        Mixins.MANY,
    componentDidMount:         Mixins.MANY,
    componentWillReceiveProps: Mixins.MANY,
    shouldComponentUpdate:     Mixins.ONCE,
    componentWillUpdate:       Mixins.MANY,
    componentDidUpdate:        Mixins.MANY,
    componentWillUnmount:      Mixins.MANY,

    # Compatibility hack
    getDefaultProps:           Mixins.MANY_MERGED,
    getInitialState:           Mixins.MANY_MERGED
  },
  defaultRule:                 Mixins.ONCE
}
React = require 'react'
{ Component, mixins } = require 'coffee-react-class'

// Define component
class MyButton extends Component

  constructor: (props) ->
    super props
    # `Component` class set `@state` from `_getInitialState()` automatically
    # If you use the built-in `React.Component` you have to call it explicitly
    # @state = _.assign({}, @_getInitialState());
  }

  # If you use the built-in `React.Component` you must declare
  # this method explicitly
  #
  # _getInitialState() {
  #   return {};
  # }

  onClick: -> @setState clicked: yes

  componentDidMount: -> console.log 'called `componentDidMount` from MyButton'

  render: -> <button onClick={this.onClick}>My button</button>

# Define some mixins
myMixin1 =
  componentDidMount: -> console.log 'called `componentDidMount` from Mixin1'

myMixin1 =
  componentDidMount: -> console.log 'called `componentDidMount` from Mixin2'

  # Objects are ignored except 'statics' and 'propTypes'
  # Ignore
  someObject: {} 

  # Merge into `MyButton.propTypes`
  propTypes:
    myProp: React.PropTypes.string

  # Merge into `MyButton`
  statics: 
    queries: {}

  # Merge into `MyButton.defaultProps`
  getDefaultProps: -> { myProp: 'myProp' }

  # Rename to `_getInitialState` to avoid React's warning
  # Call in component constructor and merge result
  getInitialState: -> { myState: 'myState'}

  # Throw error if defined in other mixin
  shouldComponentUpdate: -> yes

# Set mixins to component
mixins(MyButton, [myMixins1, myMixin2]);