This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| The pipe operator |> places the primary input in the LAST argument position | |
| Let's look at its use with List.map in a couple scenarios | |
| */ | |
| module Pipe = { | |
| let add5 = n => n + 5; /* callback func */ | |
| let listPlus5 = [1, 2, 3, 4, 5] |> List.map(add5); /* [6,7,8,9,10] */ | |
| /* Common ReasonReact recipe for mapping over React elements */ | |
| let someMappedReasonElements = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| All functions in Reason are automatically curried | |
| Let's use List.map as an example | |
| Function signature: let map: ('a => 'b, list('a)) => list('b); | |
| */ | |
| let add5 = n => n + 5; /* callback func */ | |
| let add5ToList = List.map(add5); | |
| let list = [1,2,3,4,5]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open Belt | |
| /* This is a variant of type animal */ | |
| type animal = | |
| | Cat | |
| | Dog; | |
| /* This is a record of type cat */ | |
| type cat = { | |
| name: string, | |
| species: animal /* animal can be Cat or Dog */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 1. Use stylesheets | |
| BuckleScript provides escape hatches. | |
| You can run native Javascript code with [%bs.raw {| CODE GOES BETWEEN HERE |}] | |
| */ | |
| [%bs.raw {|require('./index.css')|}]; | |
| /* 2. Use inline styles */ | |
| let styles = ReactDOMRe.Style.make(~backgroundColor="peach", ~width="500px", ()); | |
| <div style=styles />; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* This is a variant of type animal */ | |
| type animal = | |
| | Cat | |
| | Dog; | |
| /* This is a record of type cat */ | |
| type cat = { | |
| name: string, | |
| species: animal /* animal can be Cat or Dog */ | |
| /* color: string */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Like proptypes, you define your state type */ | |
| type state = {counter: int}; | |
| /* Define a Redux-like action variant here */ | |
| type action = | |
| | Count(int); | |
| /* Define the reducer component */ | |
| let component = ReasonReact.reducerComponent("SomeStatefulComponent"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* You can pattern match against a variety of data types: | |
| tuples, arrays, variants, strings, etc. | |
| */ | |
| /* Define a laptop variant */ | |
| type laptop = | |
| | Macbook | |
| | Chromebook | |
| | Surface(string); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* This is a variant of type animal */ | |
| type animal = | |
| | Cat | |
| | Dog; | |
| /* This is a record of type cat */ | |
| type cat = { | |
| name: string, | |
| species: animal, /* animal can be Cat or Dog */ | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ChildComponent = { | |
| let component = ReasonReact.statelessComponent("ChildComponent"); | |
| /* You must declare your props as labeled arguments with the ~ symbol */ | |
| /* If I don't, compile error: It only accepts 1 argument; here, it's called with more. */ | |
| let make = (~randomTextProp, _children) => { | |
| ...component, | |
| render: _self => <p> {ReasonReact.string(randomTextProp)} </p>, | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* ReasonElements.re */ | |
| let component = ReasonReact.statelessComponent("ReasonElements"); | |
| let make = _children => { | |
| ...component, | |
| render: _self => { | |
| let someListOfReactElements = [<div />, <div />, <div />]; | |
| let someArrayOfReactElements = Array.of_list(someListOfReactElements); | |
| let isNull = true; |