Clear Completed
So let's just start back up at the top at the TodoApp level. And we're just going to do a clearCompleted. Your stickers are slowing you down. That's what Tusijia said. You're trying to figure out what I have? I wish I had a React sticker so bad. I don't. I actually bought an Ember sticker, because I caught a lot of (bleep) for giving workshops on Ember and not having an Ember sticker, so I went and bought one. She's giving you crap for all those stickers are slowing you down. That's probably true as well. I'm guessing I probably should read that. Just kidding. All right newTodos, var newTodos = this.state.todos.filter, again, this is like the third time you've seen this, so none of this should look super unfamiliar to you. Okay, return.el.completed, guess you need that. Bang, right there. And this.setState({todos: newTodos}). So as you can see, you can write setState methods just like that. That works as well. As opposed to, I mean, grabbing the state and resetting it however you... This works as well. Guess we can take this console.log out there. We don't need that. Okay, let's go down here to this. ClearCompleted needs its own little function right there. So clearComplete = (this.clearCompleted). And let's go down to the Clear Completed, right there. And this one, we can actually write out, it's pretty small, Clear Completed, Okay and close your /button, /div, that's all the markup we need. Let's go add a handleClick: function, takes an event, comma, and we're just going to do this.props.filter. We'll just call it clearCompleted. That's all it needs to do. Then down here on the onClick, we're just going to say onClick={this.handleClick}, Save that. Run back over here, and hopefully clearCompleted wipes out all our completed stuff. So even if you like add new stuff and then still clear completed. That's it. That's the entire React app right there. So let's talk about, in specifically, notice that this is 163 lines of code. You go look at our Angular app, it's probably 30 or 40 lines of code. So one's inclination, at least my initial inclination is to say, what the (bleep) would I care about React for? I want to write less code. React, while it may be a little bit more verbose to get up and running, because everything modifies the code lives next to it, whenever that code is modifying data in a way that you don't anticipate, you know that it's always going to be next to it. It's always going to be right next to where that data lives. That's really powerful when you're working in a big team, or even a small team, or even dealing with yourself three months from now, that you know it makes your code very predictable, and very easy to find where bugs are. It just makes that area very small where bugs can live. So I think it's a really powerful architecture. I'm a big fan and I like all these frameworks, but this is actually the one that's Reddit is moving forward with, for right now. So kind of interesting, kind of fun. And it's very, very, very performant. So all these are pretty performant. As long as they're written well, which is always a problem for everything. (laughs)
React Questions
Does anyone have any questions or comments? You tweeted that you felt like a wizard when you're using React, why is that? It was just one of those moments where, I'm trying to remember exactly what happened, but I remember that the feeling that I got was, I had written some code, and I had unintentionally reused code that I had written, and so something that I wasn't expecting yet to work had worked. And so I was like my past self had cast a spell on my future self to make code work. That's (laughs) more or less the function that I-- So the re-usability is really what-- Yeah. But you were just talking about, earlier, how directives is re-usable and how you were so excited there. Definitely. So how do you feel it compares? Directives are very re-usable. And you'll definitely get those wizardy type moments. My biggest complaint, and I've written a lot of angular, and my biggest complaint that I can leverage against Angular, is that I used to not be good at writing Angular, and I'm not implying that I'm now great at writing Angular, but my old code is awful, and it's really hard to follow. So they don't really give you a whole lot of hand in helping you structure your app. Now some people find that great. Some people that get Angular know where stuff should go and they have their opinions of how they want it to work. It took me awhile before I got that opinion. And so I have a lot of bad Angular now that I have to maintain. Whereas all the React that I've written, even the stuff that I was first starting out has very few anti-patterns in that, just because it's a bit harder to get there, just because of how they force you to let your data flow. And (mumbles) asked how are you doing your content testing in Reddit and specifically in React. It's in Jasmine. That's just kind of my flavor, that's the one that I get the best. You can use any of them. Actually Facebook has a framework called Jest, that is built on top of Jasmine, that you can use specifically for React. I think it works on other things as well. But, yeah, we're just writing unit test using Karma, using Grunt, and using Jasmine to kind of test these individual component functions. They kind of make them more functional, like functional-esque programming, so that they're just applying transformations as much as possible, as opposed to just modifying the state. So we try and separate those functional transformations away from the state mutability of it. And that makes those particular calculations and transformations very testable. And if you want to get more functional programming, on Frontend Masters, there's Brian Londsdorf, I think that's how you say his last name. His stuff is stellar, so I'd definitely check that out. Other questions? Do you find that more code required with React that takes a little more effort to keep the size of code in your head. Because there's more code, it seems like maybe a little less expressive, where you get more opportunities to do things your own way, which can make it less opinionated and harder for other people to work on each other's codes? I haven't yet found that. So the one thing to keep in mind is a big thing that's different is a lot of that logic lives in Angular's templates, so you actually have two places that you're looking at. Everything, including the markup, lives here. So that's why this is much longer. I don't know what it would be if you compared them side-by-side, including their indexes.html, going to bet Angular's still a bit shorter. Angular's very expressive for that reason, but you are splitting your stuff across multiple places. Like React, everything for the createNewTodo thing is all right there. You cannot go outside of there, except, I guess, in that top TodoApp, including its parents, right? You have only that ancestry to look at. And I find it sufficiently and not excessively expressive. (laughing) I feel like that's a cop-out answer. I don't have any issues with it, though. Yeah? The fact that the presentation logic, or the presentation is all wrapped here together is making me have bad memories of PHP. I know you can be more or less disciplined. How easy, or what are techniques for being more disciplined to make it so all the presentation specific stuff is not totally trampling everything, and so that it's a little easier to change and keep separate? That is a widely and often spoken criticism of React is that it feels PHP-esque, in that, not that there's anything wrong with great PHP. Great PHP is a great language, but that markup, and that functionality is living next to each other, which our developer lives we've been told that that's an anti-pattern, right? That you want to separate your concerns out. The reason why I've suspended that hesitation is that the way that they've done it makes sense to me. Kind of going back to this idea that everything just lives right there. Rather than grouping together, like these are actions, these are fuse, these are different things like that, they've said here's a totally encapsulated thing that does everything that it needs to and needs no help from no one else, other than its parents and ancestors, but whatever, however you want that to look. That makes those components very re-usable, and it makes them very portable. It's not very hard to move one component. We could take that newTodo component, and throw it on another To do List App, whereas something else we'd be adding in, and then we just have to replace the functions that it's operating on. So I've said a lot of things. I haven't exactly addressed your concern yet of what ways have I found to be more disciplined. I think that global app object has helped me be more disciplined, separating out different pages, or different types of components into different files, that kind of like logical separation of files, and such, helps them not to bleed into each other, because it makes it hard to call them from each other, which you shouldn't do, right? You should find different ways to bus messages to each other, whether that be by events, or other constructs, like maybe having common parents that then talk down to the different children. There's Facebook's Flux architecture, which has the idea of a dispatcher, which I don't want to get into right now, but that's something worth looking at, that there's essentially this dispatcher that lives above all the parents, and then dispatches different information down to the correct ones. Those would be some of my, at least, my initial reactions to that. Does that feel like I answered your question? Okay, cool. Artem's asking, "Any good resources or particularly helpful resources, aside from Facebook's main GitHub repo about React, externals templates, React and JS flavors like Coffeescript and stuff like that? I mean, just additional resources. Yeah, so you said beside their actually repository page? I'm just still going to say that that's the best one. Their tutorial's spot on, really, really, helpful. Walking through that's really helpful. I went through the ToDoMVC.com, version of their TodDo list with React, that was really helpful. Pascal wrote a really great version of React's To Do List. What else? Well, we'll go over to Coffeescript in a second. Other than that, it's still young enough that it's kind of hard to find some of those resources. There's some good blog posts on it. Hitting up their Freenode is usually pretty helpful, I've done that a couple times. And then Stack Overflow. That's probably the resources that come to my mind that I've used. Just to talk about Coffeescript for a second. I know people like Coffeescript. I'm okay with Coffeescript. I have good and bad days with it. As far as I know, there's not a good way to integrate JSX and Coffeescript together. I haven't seen one, I don't know if it exists yet. We're actually talking about writing some sweet js macros for JSX, so that we can kind of modify it. because some of this is a bit clunky and repetitive, and I feel like it can be written better. But, right now, the suite JS does not have a JSX transformer in it yet, so it's kind of a undertaking. Anyway, they don't play nice yet as far as I've seen. And I hope that gets fixed soon, for sure. Other questions? Are you using it in production? This week. I actually was supposed to finish before I left and I did not. Our new shopping cart on RedditGifts.com is going to be all in React. So it's going to be cool. (laughs)
Wrap-up
Unsolicited Opinions
Why Backbone? Dead simple to interact with a REST API. If you're kind of familiar with Rails-y kind of style, like, or Tastypie for Python style of API, Backbone works really well with those. Like the getting and the putting for like models and stuff like that with, yeah, model-style API end points I think is what the more technical term or whatever is for it. It's a very good way to organize your code, and then it just gets out of your way and says, "Okay, I've given you the little bit of structure, "now go figure it out yourself." Really bare bones, you're never going to have, Backbone is never going to be your like limiting factor for your performance, like Backbone itself, right. It's just because there's not a lot to it. And it works great in large applications. It is slow for prototyping, I would not prototype in this, in Backbone, personally. Why React, I know I have a spelling mistake up there, but yeah, it's designed for optimal efficiency. The bottle-neck is on the browser is has been the DOM, it always has been the DOM, and my guess is that in the future it will still be the DOM, and React minimizes every single interaction it can do with the DOM to help you go faster. It makes paints and like repaints and all that kind of stuff and re-flows very very efficient. That forcing of one-way flow of data makes it super helpful for debugging, it just makes it so like there can only be like four or five places that the bug can be. Whereas like it could not be sprawled out across your application. I think Angular has that problem a lot. It's very web component-esque. Which is nice, I like web components, I think it's the future. It's easy to compose components of other components. It's easy to introduce React to your current stack. It's easy to introduce like a one components or two components on a page. It does not require very large commitment. And we talked about React renders server-side at rendering. I'm super excited about this, and I know Angular is not concentrating on server-side rendering. I don't know about the other ones, but I can only guess that they're also not concerned right now with that. The fact that you can send down pre-rendered markup and then React takes over that markup as opposed to like sending down the markup, and then having React re-render everything, no, it just like takes over and then it just keeps going from there. Not only that, you don't have to learn like another server-side template. Like you don't have to have Django templates render it, and then have it go down and have React take over. That would work, but I wouldn't recommend it. Rather what you can do is you can have React render it on the server, send it down, and it's all React, it's all one file, right. Like there's no special magic about this. So super cool, really really excited about that. So Angular and Ember, they kind of scratch the same itch in different ways, and they're like perfect use cases, they're super fast for prototyping, really super awesome. They have weirdness at like large scales. Like you're going to run into weird edge cases. I've found this in particular with Angular, like there's just weird things that you find you would have never expected, but they're both pretty fast as long as you keep to good practices, you can even start working with them at larger scales. So why Angular, it's the directive. That's just the coolness, the sexiness of everything that is Angular, it lies in the directive, it's super powerful. It's a very awesome way to influence the DOM in a declarative fashion. The ease of using the plain old JavaScript object. You get to bring all your current JavaScript knowledge, and apply that to Angular, which I think is really cool. And while it's monolithic right now, Angular is kind of decoupling all of its pieces, so you can just kind of piece it together. I think that's awesome. Like if that had never happened, like if they'd left the router baked in, because it used to be baked in, Angular-UI router would have never come out, and Angular-UI router is just great, so. Ember, computed properties, the fact that you can kind of compose properties that are computed from other properties, and they're always kept in sync. It's really powerful, particularly if you're doing some expensive calculations, or things that you just need like right away, as opposed to having the paradigm of accessing a property versus accessing a property. This is a personal opinion of mine, having worked in Ember and having done lots of work in Angular, I have very weird like WTF moments with Ember. There are some moments, like when you're doing ngOptions, which is like you have a select and you want to populate its options with an array. The domain specific language on it is just frustrating. Like every single time I end up on stack overflow, and I've done it like dozens of times now. Maybe I'm an idiot, well probably an idiot, but that's okay. Angular has just weird DSL places, so. Though I believe Ember's barrier of entry is higher, I think like, once you get into it, it helps you go super fast and as fast as Angular. Like I think this particular workshop makes it seem a bit harder to get into it, it is harder to get into it I think, but once you know how everything's working, like if I essentiate a ToDo app, right, then like the ToDo router is going to exist, and the ToDo controller's going to exist, and all that stuff just is getting instantiated for you behind the scenes on the fly. Once you get that, it helps you go super fast. Ember has great docs, great docs, great community. If you make fun of Ember, Huda cats might come yell at you, but that's cool. He likes to yell at people, but their team's really great, and their docs are awesome. Angular has still to this day produced docs, in my opinion.
Extended Features
There's tons of cool things that we didn't talk about, like extended features to these frameworks. Cool things that we didn't talk about, we didn't talk about Backbone interacting with APIs, but that's a cool feature about it. And we did this with Vanilla Backbone. The great part about Backbone is the Backbone community. There's like Backbone.validation, right, like that's a really cool thing that just goes through and does like validation for you. Like there's just tons of these little Backbone things. It's like jQuery right, like there's just a jQuery plugin for everything, right. I kind of feel sometimes that way about Backbone. And Backbone seems like a lot of code, but like now that we have that ToDo app up and running, we could have just kept going and going fast, right. It's that setup that kind of sucks about Backbone. Ember, and I also apologize for the wall of text, right, but it felt better than going through like a thousand slides, so. Computer properties, like seriously, they're just like the greatest thing. We didn't actually set them up, but they're pretty easy to set up, and this is something that I haven't discussed yet, but I think is a crucial point, especially with comparing Ember and Backbone. Ember is a framework, they sit at their framework, and like it is supposed to be like the scaffolding that you build your app on. They are very particular at Angular, they say that Angular is not a framework. I kind of have my disagreements, but they're insistent that it is not a framework. It's a superheroic framework. Yeah, I mean like yeah, super, yeah, they did use to call it that, but they insist now that it is the tool set that you use to build your framework. Now I mean, we're splitting hairs, right. Like it's a very subtle difference, but it really comes down to their philosophy that Ember wants to tell you how to build your app, which is okay, right. Like they actually have really good opinions on it. They have really smart people that have done lots of these things, that have great opinions about it. Angular does not want to tell you how to build your app, it wants to help you, and it says here are your tools, now go build up like the scaffolding so you can build up your app. And like that's the directions they're heading off into, like for the future as well. And everyone says that Angular directives are amazing, but guess what, most of that stuff can be accomplished with Ember components and Ember views. Like it's not that we're doing things that can't be accomplished otherwise. The only interesting behavior that's hard to replicate is that kind of mixin directives. You can have multiple directives on one attribute that can be used across different ones. Kind of that extensible web kind of stuff. So that said, okay, directives are still really cool for that very reason that we were just talking about, is that they are just like, they're extensible, and they can be applied, and they can be combined. And you can also do like the component stuff-esque if you, like web component stuff using like the element level of directives. Filters, and we went over filters a little bit before lunch. Very powerful, and kind of the sky is the limit. Your imagination's the limit of what you can do with filters. And then custom services, like that ability to dependency inject where things are necessary is really cool and makes things super testable. Of all of these, my opinion is that Angular is most testable. So if you're super big into testing, you're probably going to be super big into Angular as well. React, we glanced over mixins just a little bit, but writing your own mixins is kind of cool. I don't have any experience in it, just kind of glanced at what other people are doing. But the ability to share functionality across unrelated components is powerful. I mean, it's kind of why we like directives, right. React is made with functional programming in mind. Like they have a lot of like core values, like referential transparency, and some of these other things like independence, and some of these other cool things that make it very functional in nature. It makes it, again testable, right, and it makes it very easy to fit into that kind of paradigm while you're writing React. So kind of cool, I mean there is mutability associated with it as well, but just keep that in mind. React, or Facebook made React with Flux in mind. I'd recommend checking out like Cortex's library or Fluxxor. More Fluxxor, because that one's actually meant to be a framework for Flux, which is you use Flux instead of MVC, right. Like they're competing paradigms, but it's pretty cool. And then the server-side rendering is just kickass and amazing. Okay, so what other questions do you have? That's my Steve Holt snoo, like that's the one that was almost my snoo, but then I switched to the other one, so. Cool, well thanks for coming, follow me on Twitter @HOLTBT, and thank you, thanks for coming.