Cheatsheet
If you are not familiar with ReScript / Reason, be sure to check ReScript Overview to learn how to interop with JavaScript
<Text>Hello, world!</Text>
<Text> "Hello, world!"->React.string </Text>
<Text>42</Text>
<Text>
42
->Js.Int.toString
->React.string
</Text>
<Text>4.2</Text>
<Text>
4.2
->Js.Float.toString
->React.string
</Text>
{items.map((item, i) =>
<Text key={i++item}>
item
</Text>
)}
{items
->Belt.Array.mapWithIndex((item, index) =>
<Text
key=((index->Js.Int.toString)++item)>
item->React.string
</Text>
)
->React.array}
<Text>{condition && something}</Text>
<Text>
{
condition ? something->React.string : React.null
}
</Text>
Assuming something
is a string
that can be undefined
.
{
something && <Text>{something.toUpperCase()}</Text>;
}
Recommended: Assuming something
is an optional string
.
{
something
->Belt.Option.map(thing =>
<Text>
thing
->Js.String.toUpperCase
->React.string
</Text>
)
->Belt.Option.getWithDefault(React.null)
}
If you have to work with JavaScript/JSON: Assuming something
is a JavaScript string
that can be undefined
.
{
something
->Js.Nullable.toOption /* convert undefined || string as option(string) */
->Belt.Option.map(thing =>
<Text>
thing
->Js.String.toUpperCase
->React.string
</Text>
)
->Belt.Option.getWithDefault(React.null)
}
import { StyleSheet } from "react-native";
const styles = StyleSheet.create({
container: {
maxHeight: 600,
width: 800,
justifyContent: "flex-start",
alignItems: "center",
margin: "auto",
},
cornerThing: {
position: "absolute",
top: 100,
right: -20,
transform: [{ rotate: "4deg" }],
},
text: {
textTransform: "uppercase",
},
});
console.log(StyleSheet.flatten([styles.container]));
open ReactNative;
let styles =
Style.(
/* = open Style; just between ()*/
/* this is useful since all units & style methods comes from Style module */
StyleSheet.create({
"container":
viewStyle(
~maxHeight=600.->dp,
~width=80.->pct,
~justifyContent=`flexStart,
~alignItems=`center,
~margin=auto,
(),
),
"cornerThing":
viewStyle(
~position=`absolute,
~top=100.->dp,
~right=(-20.)->dp,
~transform=[|rotate(~rotate=4.->deg)|],
(),
),
"text": textStyle(~textTransform=`uppercase, ()),
})
);
Js.log(StyleSheet.flatten([|styles##container|]));
<View style={[styles.container, styles.containerAdditionalStyles]} />
<View
style=Style.(array([|
styles##container,
styles##containerAdditionalStyles
|]))
/>
<View
style={[
styles.container,
condition && styles.containerAdditionalStyles,
condition2 && { width: 40 },
]}
/>
<View
style=Style.(arrayOption([|
Some(styles##container),
condition ? Some(styles##containerAdditionalStyles) : None,
condition2 ? Some(viewStyle(~width=40.->dp, ())) : None,
|]))
/>
/* App.js */
import React, { Component } from "react";
import { Text, View } from "react-native";
export default class HelloWorld extends Component {
render() {
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}
>
<Text>Hello, {this.props.name || "world"}!</Text>
</View>
);
}
}
/* App.re */
open Belt;
open ReactNative;
[@react.component]
let make = (~name=?) => {
<View
style=Style.(
viewStyle(~flex=1., ~justifyContent=`center, ~alignItems=`center, ())
)>
<Text>
{("Hello, " ++ name->Option.getWithDefault("world") ++ "!")
->React.string}
</Text>
</View>;
};