ReasonML Quick Start Guide

Book description

A hands on approach to learning ReasonML from the perspective of a web developer.

Key Features

  • Hands on learning by building a real world app shell that includes client-side routing and more.
  • Understand Reason's ecosystem including BuckleScript and various npm workflows.
  • Learn how Reason differs from TypeScript and Flow, and how to use it to make refactoring less stressful.

Book Description

ReasonML, also known as Reason, is a new syntax and toolchain for OCaml that was created by Facebook and is meant to be approachable for web developers. Although OCaml has several resources, most of them are from the perspective of systems development. This book, alternatively, explores Reason from the perspective of web development.

You'll learn how to use Reason to build safer, simpler React applications and why you would want to do so. Reason supports immutability by default, which works quite well in the context of React.

In learning Reason, you will also learn about its ecosystem ? BuckleScript, JavaScript interoperability, and various npm workflows. We learn by building a real-world app shell, including a client-side router with page transitions, that we can customize for any Reason project. You'll learn how to leverage OCaml's excellent type system to enforce guarantees about business logic, as well as preventing runtime type errors.You'll also see how the type system can help offload concerns that we once had to keep in our heads.

We'll explore using CSS-in-Reason, how to use external JSON in Reason, and how to unit-test critical business logic. By the end of the book, you'll understand why Reason is exploding in popularity and will have a solid foundation on which to continue your journey with Reason.

What you will learn

  • Learn why Reason is exploding in popularity and why it's the future of React
  • Become familiar with Reason's syntax and semantics
  • Learn about Reason's ecosystem: BuckleScript and JavaScript interoperability
  • Learn how to build React applications with Reason
  • Learn how to use Reason's type system as a tool to provide amazing guarantees
  • Gain a solid foundation on which to continue your journey

Who this book is for

The target audience of this book is web developers who are somewhat familiar with ReactJS and who want to learn why ReasonML is the future of ReactJS.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. ReasonML Quick Start Guide
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Introduction to ReasonML
    1. What is ReasonML?
    2. Why Reason?
      1. Support for immutability and purity
      2. Module system
      3. Type system
      4. Cross-platform
      5. Maintainability
      6. Interoperability
      7. ES2030
      8. Community
      9. The Future of ReactJS
    3. Exploring Reason
      1. Data structures and types
        1. Creating our own types
        2. Pattern matching
      2. Making Invalid States Impossible
    4. Summary
  7. Setting Up a Development Environment
    1. The Reason toolchain
      1. Installing BuckleScript
      2. Editor configuration
    2. Setting up a pure Reason project
      1. The bsconfig.json file
        1. The warnings field
        2. The package-specs field
        3. The suffix field
        4. The sources field
      2. Working with DOM
    3. Setting up a ReasonReact project
      1. Improving the developer experience
    4. Summary
  8. Creating ReasonReact Components
    1. Component creation basics
      1. Component templates
      2. self
      3. Event handlers
      4. unit
      5. JSX
      6. Props
      7. Children
      8. Life cycles
      9. Subscriptions helper
    2. Stateful components
      1. State, action, and reducer
      2. Refactoring
      3. Instance variables
        1. Ref and mutable records
    3. Navigation menu
      1. Bindings
      2. Events
      3. Js.t Object
      4. Adding actions
      5. Inline styles
      6. React ref 
      7. Velocity
    4. Client-side routing
      1. Current route
      2. Helper functions
      3. Usage
    5. Summary
  9. BuckleScript, Belt, and Interoperability
    1. Module scope
    2. Data structures
      1. Array
        1. Using the Reason standard library
        2. Using the Belt standard library
        3. Using BuckleScript's built-in JavaScript bindings
        4. Using a custom binding
        5. Using raw JavaScript
      2. List
        1. Recursion
    3. Pipe operators
    4. Using Belt
      1. Option module
      2. List module
        1. make
        2. makeBy
        3. shuffle
        4. take
        5. Deck of cards example
      3. Currying
      4. Uncurried functions
        1. makeByU
    5. JavaScript interoperability
      1. Using JavaScript in Reason
        1. Understanding the [@bs.val] decorator
        2. Understanding the [@bs.scope] decorator
        3. Understanding the [@bs.send] decorator
        4. Understanding the [@bs.module] decorator
        5. Reasonable APIs
        6. BuckleScript documentation
    6. Binding to existing ReactJS components
      1. Importing dependencies
      2. Creating the make functions
        1. Using [@bs.deriving abstract]
      3. Using the components
    7. Summary
  10. Effective ML
    1. Module signatures
      1. Module types
      2. Abstract types
    2. Using module signatures
    3. Phantom types
    4. Polymorphic variants
      1. Would this work with normal variants?
    5. Advanced type system features
    6. Summary
  11. CSS-in-JS (in Reason)
    1. What is CSS-in-JS?
      1. A brief history
    2. Using styled-components
      1. Using [@bs.variadic]
    3. Using bs-css
      1. Trade-offs
      2. Other libraries
    4. Summary
  12. JSON in Reason
    1. Building views
      1. File structure
      2. Updating the router and navigation menu
    2. Integrating with localStorage
      1. Populating localStorage
        1. Validating JSON strings 
        2. Using Js.Json.classify
      3. Writing to localStorage
    3. Using bs-json
    4. Using GraphQL
      1. What is GraphQL?
      2. When using GraphQL, do I need to create JSON decoders?
    5. Summary
  13. Unit Testing in Reason
    1. Testing with Jest
      1. Installation
      2. Our first test
      3. Testing business logic
      4. Reflecting
    2. Summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: ReasonML Quick Start Guide
  • Author(s): Raphael Rafatpanah, Bruno Joseph D'mello
  • Release date: February 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789340785