2018 - React Chicago - ReasonReact - A Love Story

2018 - React Chicago - ReasonReact - A Love Story

When I first met ReactJS, it was love at first sight. We built beautiful UIs for years together with our firstborn, Redux. I felt like nothing would get between us. Not even when a younger, skinnier version came along (Preact). Life was good.

Everything changed when ReasonReact showed up on our block. I was curious, of course. Didn’t act on it, though. Why would I? We’d run into each other a lot since we shared the same circles. One day, the temptations got too strong. What was I thinking? Nothing about what I was doing felt right, at first. I was leaving a lot behind but for some weird reason, I knew I’d end up here anyway. I mean, it’s not like we’re splitting apart. React will still be there, but so will Reason...

I’m here to announce my love affair.

Peter Piekarczyk

June 27, 2018

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

62 slides
  1. What is ReasonML • New language by the creator of

    React • Familiar JavaScript syntax on top of OCaml AST • Battle-tested language & ecosystem • Friendly compiler errors & warnings
  2. JavaScript as a statically typed, functional language with a super

    fast compiler & amazing error messages What is ReasonML
  3. • Compiles 10x faster than Babel • Eliminates typical Javascript

    errors • Use it alongside your current JS apps • Compiles down to native binaries • Readable compiled JavaScript Why Consider Reason
  4. Messenger.com •Full rebuild (100s of files) is 2 seconds •Incremental

    builds are < 100ms •10 bugs for the whole year vs every week •Refactoring reduced to minutes instead of days
  5. let fizzbuzz = (i) !=> switch (i mod 3, i

    mod 5) { | (0, 0) !=> "FizzBuzz" | (0, _) !=> "Fizz" | (_, 0) !=> "Buzz" | _ !=> string_of_int(i) };
  6. function fizzbuzz(i) { var match = i % 3; var

    match$1 = i % 5; if (match !!!== 0) { if (match$1 !!!== 0) { return String(i); } else { return "Buzz"; } } else if (match$1 !!!== 0) { return "Fizz"; } else { return "FizzBuzz"; } }
  7. let rec factorial = (n) !=> n !<= 0 ?

    1 : n * factorial(n - 1);
  8. var Caml_int32 = require("stdlib/caml_int32"); function factorial(n) { var match =

    n !<= 0; if (match) { return 1; } else { return Caml_int32.imul(n, factorial(n - 1 | 0)); } }
  9. let test = () !=> { let m = ref(IntMap.empty);

    let count = 1000000; for (i in 0 to count) { m !:= IntMap.add(i, i, m^); }; for (i in 0 to count) { ignore(IntMap.find(i, m^)); }; };
  10. var Map = Immutable.Map; var m = new Map(); var

    test = function() { var count = 1000000; for(var i = 0; i < count; !++i) { m = m.set(i, i); } for(var j = 0; j < count; !++j) { m.get(j); } } test();
  11. Compile Targets ReasonML syntax
 (.re, .rei) OCaml syntax
 (.ml, .mli)

    OCaml AST JavaScript Native Code Bytecode ocamlc ocamlopt BuckleScript reasonmlhub.com
  12. • Established Language (1996) • Used in high-performance apps •

    Facebook uses it for everything (Flow, Hack) • First version of React was in Standard ML Why OCaml
  13. • Babel became a popular thing • Bloomberg released BuckleScript

    • Jordan rewrote React in OCaml • Believed the world was ready ReasonML History
  14. • First versions of React were written in StandardML •

    Babel didn’t exist • Adoption would have been hard • ReactJS was born! React History
  15. Write safer & simpler code
 in OCaml or Reason
 that

    compiles to JavaScript BuckleScript
  16. Bindings type t; [@bs.get] external hairlineWidth : t !=> float

    = "hairlineWidth"; let hairlineWidth = hairlineWidth(t);
  17. Types • Can be inferred • Coverage is *always* 100%

    • Completely sound • Consume arguments like a function
  18. Types type intCoordinates = (int, int); type coordinates(‘a) = (‘a,

    ‘a); let intCoordinates = coordinates(int); let house: coordinates(float) = (0.12, -0.12); let house = (0.12, -0.12);
  19. Variants type myResponseVariant = | Yes | No | PrettyMuch;

    let areYouCrushingIt = Yes; this OR that OR that
  20. Pattern Matching let message = switch (areYouCrushingIt) { | No

    !=> "No worries. Keep going!" | Yes !=> "Great!" | PrettyMuch !=> "Nice!" }; “Great!”
  21. Pattern Matching let message = “sup"; let reply = switch

    (message) { | "Reason's pretty cool" !=> "Yep" | "good night" !=> "See ya!" | “hello" | "hey" !=> "hello to you too!" | _ !=> "Nice to meet you!" }; “Nice to meet you!”
  22. Modules • Treated as mini files • Can include types

    & nested modules • Can be “opened” and “extended”
  23. Modules module School = { type profession = Teacher |

    Director; let person1 = Teacher; let getProfession = (person) !=> switch (person) { | Teacher !=> "A teacher" | Director !=> "A director" }; }; let anotherPerson: School.profession = School.Teacher;
  24. • Write React components using Reason • Not a re-write,

    just the bindings • Safe & statically typed: it just works! ReasonReact
  25. Your First Component let component = ReasonReact.statelessComponent("Greeting"); let make =

    (~name, _children) !=> { !!...component, render: (_self) !=> <button> {ReasonReact.string("Hello " !++ name !++ “!")} !</button> };
  26. Arguments let make = (~name, _children) !=> { !!...component, render:

    (_self) !=> <button> {ReasonReact.string("Hello " !++ name !++ “!")} !</button> };
  27. Arguments let make = (~name, _children) !=> { …ReasonReact.statelessComponent(“Greeting"), render:

    (_self) !=> <button> {ReasonReact.string("Hello " !++ name !++ “!")} !</button> }; DO NOT DO THIS!!!
  28. • npm install -g bs-platform • add bsconfig.json • bsb

    -make-world -clean-world Existing Project
  29. bsconfig.json { "name": “my-project”, "reason": {"react-jsx" : 2}, "bsc-flags": ["-bs-super-errors"],

    "package-specs": [{"module": “commonjs", "in-source": true}], "suffix": ".bs.js", "namespace": true, "bs-dependencies": ["bs-react-native","reason-react"], "sources": [{"dir": "re"}], "refmt": 3 }

More Decks by Peter Piekarczyk

Other Decks in Technology