Slide 1

Slide 1 text

UNDERSTANDING
 JAVASCRIPT VIA REACT DJANGOCON US 2017

Slide 2

Slide 2 text

$ whoami ANDREW PINKHAM ▸ Author of Django Unleashed ▸ Founder of JamBon Software ▸ CertCycle

Slide 3

Slide 3 text

UNDERSTANDING
 JAVASCRIPT VIA REACT DJANGOCON US 2017

Slide 4

Slide 4 text

OUTLINE TALK OUTLINE ▸ How did this happen? ▸ Brief History of the Web ▸ JavaScript (and HTML) ▸ JavaScript Frameworks (including React) ▸ React's Ecosystem ▸ React and Django ▸ The Future

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

HOW DID THIS HAPPEN TASKED WITH: ▸ Learn JavaScript ▸ Learn the JavaScript Ecosystem ▸ Pick a Framework ▸ Build a dynamic frontend website to communicate with an API via REST (the Django bit)

Slide 7

Slide 7 text

I DID NOT
 UNDERSTAND THE PROBLEM

Slide 8

Slide 8 text

HOW DID THIS HAPPEN DJANGO ▸ I need to build a website. What should I use? ▸ Oh, you should use Django.

Slide 9

Slide 9 text

HOW DID THIS HAPPEN DJANGO ▸ I need to build a a software application that communicates with other computers over the HTTP protocol by providing HTML and CSS documents to those computers. What should I use? ▸ I recommend the Django framework, because it supplies all the code you need to communicate via HTTP, and it provides methods to rapidly produce HTML and package CSS for that HTML.

Slide 10

Slide 10 text

HOW DID THIS HAPPEN JAVASCRIPT ▸ I need to build a dynamic frontend. What should I use? ▸ Oh, you should use Angular/React/Vue/RxJS/CycleJS.

Slide 11

Slide 11 text

WHAT IS THE PROBLEM?

Slide 12

Slide 12 text

BRIEF HISTORY OF THE WEB HTML ▸ HTML first appears in 1991 ▸ Mosaic, Netscape Navigator, Internet Explorer ▸ HTML is intended to share documents: provides the Document Object Model (DOM), directed graph of nodes ▸ HTML provides the content and structure of a document ▸ Loosely analogous to Model (content) and View (structure) ▸ CSS is first proposed in 1994; spec released in Dec 1996

Slide 13

Slide 13 text

BRIEF HISTORY OF THE WEB JAVASCRIPT ▸ JavaScript built in 10 days as "glue language" to cement partnership between Sun and Netscape against Microsoft in 1995 ▸ Brendan Eich wants to put Scheme in the browser; politics give it Java-like syntax and the name (JavaScript). Prototypal inheritance is taken from Self. ▸ JavaScript is built to manipulate the DOM ▸ JavaScript spec changes over time

Slide 14

Slide 14 text

BRIEF HISTORY OF THE WEB THE PROBLEMS ▸ JavaScript redefines known vocabulary and is better conceptualized as a functional language ▸ JavaScript is implemented in different browsers with slightly different features ▸ HTML's DOM is a directed graph with it's own hidden state and event handling; HTML differs across browsers ▸ HTML is both the state and display of the program

Slide 15

Slide 15 text

JAVASCRIPT JAVASCRIPT QUIRKS ▸ A human/developer problem ▸ Read Douglas Crockford's "JavaScript: the Good Parts" ▸ Read Eric Elliott's "Programming JavaScript Applications" ▸ Read Kyle Simpson's JavaScript Series ▸ FrontEnd Masters and Egghead.io

Slide 16

Slide 16 text

JAVASCRIPT JAVASCRIPT IN DIFFERENT BROWSERS ▸ JQuery ▸ Mootools

Slide 17

Slide 17 text

JAVASCRIPT DIFFERENT LANGUAGE VERSIONS ▸ EcmaScript (ES) and TC39 ▸ ES 3 or 5 in browsers ▸ ES 6 and 7... but you can't upgrade the browser! ▸ Write ES 6 or 7, but ship ES 5? ▸ Compiler JS to JS - "transpilation" ▸ Bundle namespaced JS into single file

Slide 18

Slide 18 text

JAVASCRIPT TRANSPILERS ▸ Babel JS for ES 6 to ES5 ▸ Bublé ▸ CoffeeScript and TypeScript

Slide 19

Slide 19 text

JAVASCRIPT BUNDLES [AND PIPELINES] ▸ [Grunt or Gulp] and Browserify ▸ Webpack (for apps) ▸ Rollup (for libraries)

Slide 20

Slide 20 text

JAVASCRIPT ENVIRONMENT TOOLS ▸ Package Managers ▸ NPM ▸ Yarn ▸ PNPM ▸ Linters ▸ JSLint and JSHint ▸ ESLint

Slide 21

Slide 21 text

JAVASCRIPT STARTING STACK ▸ Babel for ES 6 transpilation ▸ Webpack for bundling ▸ ESLint for linting ▸ Yarn for package management? NPM has fixed the issue? What about PNPM?

Slide 22

Slide 22 text

MAKE SURE WE'RE ON THE SAME PAGE NOMENCLATURE ▸ Library: a codebase that you call from your code (requests) def main():
 r = requests.get(
 'http://placekitten.com/300/300') ▸ Framework: a codebase that calls your code (Django) def a_django_view(request):
 return render(request, 'template.html')


Slide 23

Slide 23 text

THIS IS YOUR REMINDER THAT THE DOM IS ACTUALLY A GIANT, MUTABLE, GLOBAL VARIABLE IN THE MIDDLE OF YOUR PROGRAM. @polotek Marco Rogers BRIEF HISTORY OF WEB

Slide 24

Slide 24 text

JS FRAMEWORKS VANILLA JS M V C HTML JavaScript

Slide 25

Slide 25 text

JS FRAMEWORKS MODEL VIEW CONTROLLER ▸ Angular Framework ▸ Referred to as MVC ▸ JavaScript is a separate Controller ▸ Annotate HTML with custom tags (directives) to split single variable (DOM) until smaller scopes ▸ Two-way binding ▸ Content is dynamic; structure of app still in HTML

Slide 26

Slide 26 text

JS FRAMEWORKS ANGULAR V C HTML Angular

Slide 27

Slide 27 text

JS FRAMEWORKS REACTIVE PROGRAMMING ▸ RxJS and CycleJS ▸ Streams or Observables ▸ Inputs over time ▸ Functions defined at beginning that operate over time

Slide 28

Slide 28 text

JS FRAMEWORKS CYCLE AND REACTIVE JS V C HTML JavaScript Actions
 over Time

Slide 29

Slide 29 text

JS FRAMEWORKS FACEBOOK'S REACT ▸ React Library ▸ Said to be the View in an MVC app ▸ Defines a "Flux Architecture" ▸ Virtual DOM ▸ Single direction of information

Slide 30

Slide 30 text

JS FRAMEWORKS REACT V C HTML React
 Virtual Dom State M

Slide 31

Slide 31 text

JS FRAMEWORKS REACT V M React

Slide 32

Slide 32 text

JS FRAMEWORKS REACT

Slide 33

Slide 33 text

REACT ECOSYSTEM WHAT TO USE WITH REACT ▸ JSX - Syntactic sugar to declare HTML ▸ Redux & React-Redux (Reducer/Accumulator)

Slide 34

Slide 34 text

JS FRAMEWORKS REACT V M React & JSX Redux & React-Redux

Slide 35

Slide 35 text

REACT ECOSYSTEM WHAT TO USE WITH REACT ▸ Redux Thunk (Asynchronous Actions) ▸ Fetch Polyfill (XMLHttpRequest Replacement) ▸ Redux Forms (HTML form handling) ▸ Redux Logger

Slide 36

Slide 36 text

REACT ECOSYSTEM TESTING WITH REACT ▸ Browser vs Node ▸ Browser: Karma with Jasmine or Mocha ▸ Browser: Webdriver.io ▸ Node: Jest

Slide 37

Slide 37 text

DJANGO AND REACT WHO CONTROLS THE DOM? ▸ Django API ▸ Easy ▸ Django Templates ▸ but React owns the DOM ▸ Precomputing React? ▸ Webpack bundles? (for cache busting) ▸ Watch Julien Phalip's talk about React and Django

Slide 38

Slide 38 text

CONCLUSION JAVASCRIPT'S PROBLEM IS THE DOM ▸ Frameworks seek to provide a mental framework to allow developers to avoid the perils of modifying a global variable that also counts as the structure and display of the app ▸ Appears to me to be an increasing tendency to provide a single direction, instead of two-way bindings ▸ However, still good reasons to use two-way binding frameworks, as they may provide better solution for your interface

Slide 39

Slide 39 text

STARTING ANEW AVOID ALL THIS CONFIG ▸ create-react-app ▸ Yarn compatible, Webpack & Babel, ESLint, and Jest ▸ JSX and React ▸ Redux must be installed

Slide 40

Slide 40 text

JAVASCRIPT FUTURE WHAT'S COMING NEXT? ▸ Performance: InfernoJS and Svelte ▸ React Fiber ▸ But what of React patents and OSS? ▸ Vue JS?

Slide 41

Slide 41 text

THANK YOU! @andrewsforge http://andrewsforge.com