r/javascript • • Dec 15 '15

Angular 2 Beta is now available

http://angularjs.blogspot.com/2015/12/angular-2-beta.html
140 Upvotes

88 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Dec 16 '15

[deleted]

4

u/jbscript Dec 16 '15 edited Dec 16 '15

That React example is broken (the event handlers aren't binding to this and it's putting <li> inside <li>) and it's not doing the same thing as the Riot version - here's a working version which is a better functionality match:

Todo.js

import React from 'react'

export default React.createClass({
  getInitialState() {
    return {items: []}
  },

  handleSubmit(e) {
    e.preventDefault()
    let input = e.target[0]
    this.setState({items: this.state.items.concat(input.value)})
    input.value = ''
  },

  render() {
    return <div>
      <h3>TODO</h3>
      <ul>
        {this.state.items.map((item, i) => <li key={i}>{item}</li>)}
      </ul>
      <form onSubmit={this.handleSubmit}>
        <input/>
        <button>Add #{this.state.items.length + 1}</button>
      </form>
    </div>
  }
})

index.js

import React from 'react'
import {render} from 'react-dom'
import Todo from './Todo'
render(<Todo/>, document.querySelector('#app')

From what I've played with of it Riot is a good pick for projects which don't need the power and flexibility which comes with building your UI in code - there's always Mithril.js if you want that functionality in a lighter package. Vue.js is also worth a look if you like Riot's style, as its build tools let you define each component in one file in a similar manner.

The great thing about component-based stuff taking off is that as long as the data flow is mostly top-down, it's not that onerous to try a chunk of your app out in another library/framework.

2

u/losvedir Dec 16 '15

For those following along at home, this also switches the React from ES2015 style to the original style of building components.

That's because React.createClass automatically binds the this for its callbacks, which is helpful since that's what you usually want, but also somewhat magical. I personally prefer the ES2015 way where you have to be more explicit:

class Todo extends React.Component {
  // ...
  render () {
    <form onSubmit={this.handleSubmit.bind(this)}>
  }

This way explicitly binds the handleSubmit function to use the component's this, so the function can do something like this.setState(). Alternatively, you can use ES2015's arrow function which bind's this for you:

class Todo extends React.Component {
  // ...
  render () {
    <form onSubmit={() => this.handleSubmit}>
  }

Kind of weird at first, but you get used to it and then it feels more "javascripty".

1

u/jbscript Dec 16 '15 edited Dec 16 '15

I switched it as I don't currently see any benefit from using the ES6 class syntax, you end up either having to do more work in a constructor, or recreating event handlers on each render.

I was starting to do this sort of thing when initially porting some components over:

class Todo extends Component {
  constructor(props) {
    super(props)
    this.handleSubmit = this.handleSubmit.bind(this)
  }
  // ...
}

It's a smell to me that people are enabling experimental features in Babel solely to make the syntax of equivalent components tolerable with React.Component, particularly for statics and methods bound to the instance.

1

u/losvedir Dec 16 '15

or recreating event handlers on each render

Ah, interesting, what do you mean by this?

Do you think long term react is moving towards ES6, though? I'm new to react, and was under the impression that that was the direction the project was headed, so have been getting used to the new syntax. Have they made statements either way about long term support of both syntaxes?