Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Vue over React

A Vue over React

A comparison of Vue.js and React ecosystems and principles, from a personal perspective.

Andrei Pfeiffer

January 25, 2017
Tweet

More Decks by Andrei Pfeiffer

Other Decks in Technology

Transcript

  1. App Bootstrap import React from 'react'; import ReactDOM from 'react-dom';

    import App from './App'; ReactDOM.render( <App />, document.getElementById('root') ); import Vue from 'vue' import App from './App' new Vue({ el: '#root', template: '<App/>', components: { App } })
  2. Component types React.createClass() Vue.component() › Single File Components ES2015 Classes

    › Functional Components Stateless Functional Components › › › ›
  3. Vue.component() Vue.component('Counter', { template: ` <div> <span>{{ nr }}</span> <div>

    `, data() { return { nr: 0 } } }) plain Vanilla JavaScript › great for prototypes & small projects ›
  4. Single File Components (SFC) <template> <div> <span>{{ nr }}</span> <div>

    </template> <script> export default { data() { return { nr: 0 } } } </script> <style> /* CSS goes here */ </style> compiler required (webpack / browserify) › useful for large apps › template engine support (Jade / Pug) › .vue files ›
  5. Single File Components (SFC) <template> <div> <span>{{ nr }}</span> <div>

    </template> <script src="Counter.js"></script> <style src="Counter.css"></style> external files for CSS / JS ›
  6. Functional Components suitable for containers › React-like render() function ›

    no internal state › Vue.component('Counter', { functional: true, render(createElement, context) { return createElement(/**/) } }) faster to render ›
  7. Conditionals return( <div> { cond && <span>No data</span> } </div>

    ); <template> <div> <span v-if="cond">No data</span> </div> </template>
  8. Conditionals let markup; if (cond) markup = <span>No data</span>; return(

    <div> { markup } </div> ); <template> <div> <span v-if="cond">No data</span> </div> </template>
  9. Loops return( <ul> {list.map(item => <li>{item}</li> )} </ul> ); <template>

    <ul> <li v-for="item in list"> {{item}} </li> </ul> </template>
  10. Data binding render() { return( <input value={this.name} onchange={this.update.bind(this)}/> ); }

    update(event) { this.setState({ name: event.target.value }); } <template> <input :value="name" @change="update"/> </template> <script> export default { methods: { update(event) { this.name = event.target.value; } } } </script>
  11. Data binding <template> <input v-model="name" /> </template> render() { return(

    <input value={this.name} onchange={this.update.bind(this)}/> ); } update(event) { this.setState({ name: event.target.value }); }
  12. Props validation export default { props: { name: String, age:

    { type: Number, default: 18 } } } class Foo extends React.Component { } Foo.propTypes = { name: React.PropTypes.string, age: React.PropTypes.number, }; Foo.defaultProps = { age: 18 };
  13. Custom methods export default { mounted() {}, methods: { getData()

    {}, saveData() {} } } class Foo extends React.Component { componentDidMount() {} getData() {} saveData() {} }
  14. Methods, Computed, Watchers export default { methods: { saveData() {}

    }, computed: { fullName() {} }, watch: { firstName() {} } } methods are callable functions › computed are reactive properties › watchers are similar to Angular ›
  15. Context export default { template: `<a @click="save" />`, methods: {

    save() {} } } class Foo extends React.Component { constructor() { this.save = this.save.bind(this); } render() { return (<a onclick={this.save} />); } save() {} }
  16. Components communication // parent component template: '<Child @save="saveData" />' methods:

    { saveData() {} } // parent component saveData() {} render() { return (<Child save={this.saveData}/>); } // child component render() { return (<a onclick={this.onClick} />); } onClick() { this.props.save(); } // child component template: '<a @click="onClick" />' methods: { onClick() { this.$emit('save') }, }
  17. Components communication Parent Child handler passes what to execute executes

    Parent Child notify subscribes & executes notifies
  18. Event Bus // global Event Bus const eventBus = new

    Vue() // some deeply nested component methods: { onClick() { eventBus.$emit('save') } } // any other component mounted() { eventBus.$on('save', () => {}) } $emit $on Event Bus
  19. Paradigms no state change outside store › mutable › better

    integrated › strict immutable › uex additional integration needed › optional strict mode › more boilerplate › more magic ›
  20. State update uex state.list.splice(idx, 1, data) return { ...state, list:

    [ ...state.list.slice(0, idx), data, ...state.list.slice(idx + 1) ] };
  21. // init state as immutable data import {List, Map} from

    'immutable'; const state = Map({ list: List([...]) }); // in reducer return state.setIn(['list', idx], data); Immutable.js specific / big API › serialize / deserialize needed ›
  22. CSS Animations export default { template: ` <transition name="slide"> ...

    </transition> ` } import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; class Foo extends React.Component { render() { return ( <ReactCSSTransitionGroup transitionName="slide" transitionEnterTimeout={500} transitionLeaveTimeout={500} ... </ReactCSSTransitionGroup>); } }
  23. Unit Testing react-addons-test-utils is horrible › decent out-of-the box experience

    › need to render elements on DOM › Enzyme is awesome › Jest is awesome › non-trivial to run tests on Node.js ›
  24. DevTools separate addons for React & Redux › only one

    plugin for Vue + Vuex › less features › Redux devtool more advanced ›
  25. Optimizations required key={} for iterations › default key out-of-the-box ›

    shouldComponentUpdate() › optimized out-of-the-box ›
  26. Change detection Dirty checking Doesn’t know what changed, iterates through

    all watchers Virtual DOM Depends on tree diff, needs manual optimizations Zones, Change Detection Tree Depends on CD, needs manual optimizations Virtual DOM + Dependency Tracking https://www.youtube.com/watch?v=Ag-1wmHWwS4 https://www.youtube.com/watch?v=r4pNEdIt_l4 Dependency Tracking Slower init, updates only what has changed
  27. › “good” magic, optimized out-of-the-box some magic, bigger API ›

    › requires extra boilerplate / optimizations › functional, immutable data lower level, simpler concepts, no magic › › doesn’t feel JS friendly reactive, mutable data › › feels friendlier with JS write only JS › separate HTML/CSS/JS › › smaller file size › larger file size
  28. Q?