Margarita
Krutikova
Kyrylo
Yakymenko
a functional programming language
React bindings for ReasonML
compiles ReasonML to JavaScript
syntactic extension of
Allows writing React applications in ReasonML
"Babel" for ReasonML
Constraints
Flexibility
Guarantees
"Sweet Spot of Pragmatism"
let multiply = (a, b) => a * b;
let welcome = name => "Welcome, " ++ name;
let veggies = ["pumpkin", "kale", "broccoli"];
type response =
| Fetching
| Error(string)
| Success(user);
switch (response) {
| Fetching => <Spinner />
| Error(err) => <Error err />
| Success(user) => <MyPages user />
}
type Response = {
error: string | null,
user: User | null,
isLoading: boolean
}
if (response.loading) {
...
}
if (response.error) {
...
}
if (response.user) {
...
}
switch (action.type) {
case "FETCHING":
return "Fetching...";
case "ERROR":
if (action.errorCode === 401) {
return "Unauthorized!";
}
if (action.errorCode === 404) {
return "Not found!";
}
return "Unknown error...";
case "SUCCESS":
return action.data;
}
switch (action) {
| Fetching => "Fetching..."
| Error(401) => "Unauthorized!"
| Error(404) => "Not found!"
| Error(_) => "Unknown error..."
| Success(data) => data
}
let url = ReasonReactRouter.useUrl();
switch (url.path) {
| ["cart"] => <CartPage />
| ["products", "sale"] => <CampaignPage />
| ["products", id] => <ProductPage id />
| _ => <NotFound />
};
type option('a) = None | Some('a)
No null / undefined
Safe alternative to concept of nonexistent value
let maybeInt = Some(45);
let maybeUser = Some({ name: "John" });
let maybeUser = Some({ name: "John" });
switch (maybeUser) {
| Some(user) => "Hello, " + user.name
| None => "Please, log in"
};
❌ Cannot read property of 'undefined',
'undefined' is not an object, null is not an object,
‘undefined’ is not a function (c) Javascript
Fixed fields
Immutable
Very fast
Compile to arrays 🤯
type user = { name: string, age: int };
let john = {
name: "John",
age: 42
};
john.name = "Bob";
let bob = { ...john, name: "Bob" };
No import necessary
Module name is file name
Compiler automatically searches for modules
Requirement: unique file names per project
Folder structure doesn't matter
import { Button } from '../../commmon/Button'
import { makePretty } from '../../../utils'
<Button text={Utils.makePretty(ugly)} />
* Button.re and Utils.re are somewhere in the project
reason-language-server
+
refmt
Tooling not on par with JS/TS
Sometimes tricky to find up-to-date libraries/bindings
No need to go all in from the start
Example apps built with
ReasonReact
Hasura GraphQL
Netlify
MaterialUI
CSS-in-JS
Reason Gothenburg Meetup