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

22

u/griffonrl Dec 15 '15

Too late.

19

u/Doctuh Dec 15 '15

JavaScript Famework ADD. React has gained some traction and may....Ooooh shiny!

8

u/AshamedOfYou Dec 16 '15

is anyone jumping on the riot bandwagon? Aside from that blog post I haven't seen anything about it. I get that JS is changing quickly but in my mind things are settling down as tools get more mature.

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?

1

u/vinnl Dec 16 '15

I wouldn't expect it to be the next big thing. If you look at things that got big in the past few years (first jQuery, then Angular, now React), they all brought a fairly radical new way of programming to the table. Riot only has, AFAICT, things with the level of impact of a different syntax.

The only thing presently available that I've heard of and see as having a small chance of becoming a game changer is Cycle.js, but then again, that might still be too similar to React.

27

u/[deleted] Dec 15 '15

Too little too late. We already migrated a large production app from Angular 1.x to React.

15

u/[deleted] Dec 15 '15

This is exactly what happened to me, however I'm glad react is fucking great.

1

u/buttonkop666 Dec 16 '15

Really. I'd be interested to hear how React handled input sanitization, xss security, routing, service decorators, inversion of control/dependency injection, abstraction of browser globals to enable better unit testing, and maybe about 100 other things we're using in our Angular 1 app, because I haven't seen any of that stuff in the React docs.

4

u/[deleted] Dec 16 '15 edited Feb 11 '17

kt>;bSpMt-tXPVw,5[3EnA3HuLm+W<TT1<eb5lh0391:LuDWGpGz!<R62vHTa75>1hJ!4tZB1kmA.VeXJO76vX<.[licK3&N]mi;&Ke<Au+xF6X-ZEmHeQ3#K(WhdF+:FCNQ9].ZB4^TLeVqc>F<0T,9eMRn,X&PVT&1ro95)#vmx9GUzM)Xrp5!n!FoO>EE[a-Da>C&iytbh

3

u/[deleted] Dec 16 '15

Poor browser support from Auerlia. We are considering Vue.js

3

u/[deleted] Dec 16 '15 edited Feb 11 '17

&O+4$sg6k.M7&buM~h(gV3DS)(Snw2A655h!>tT$PiE>c*X%1w-~>E88JbM&X%TPL(B+28ApJPC+<23T[X,Qz1ER)>v+d57.]O8HAP#X)lcIT&T67Ru(TD3OQDs0NME5a:2i>rhuoDes8md:M8Vq!h5lGXrsKG)qXC(L[

2

u/[deleted] Dec 16 '15

1

u/[deleted] Dec 16 '15 edited Feb 11 '17

ToS6-3w49n;KO+nmDARNm7XfpC2mIEpkMV5eN4pw90)c(W4WaNevu[-J,3uq.WqTJ%ocQb[3WzupPKeg(0b[.030kJ-L#[EI-wQBi6LBux(fGdE6v(<19]6CT

0

u/[deleted] Dec 16 '15

[deleted]

→ More replies (1)

1

u/xlr8__ Dec 16 '15

As another comment says you don't need most of this shit in React. React doesn't need an headless browser in order to run tests for instance, unlike AngularJS 1.

→ More replies (2)

10

u/techsin101 Dec 15 '15

i agree angular may never recover now.

3

u/wreckedadvent Yavascript Dec 15 '15

Is it? Last I recall, this is around the date that they were shooting for to start releasing angular 2.0.