I want to keep this thread active with small hints and tips for ReasonReact good practices.
Feel free to contribute with more snippets!
Dealing with options within JSX
Do
<>
{
data##user
->Belt.Option.map(user => <p>{React.string(user##name)}</p>)
->Belt.Option.getWithDefault(React.null)
}
</>
Don’t
<>
{
data##user
->Belt.Option.getWithDefault({ "name": "" })
->(user => {
<p>{React.string(user##name)}</p>
})
}
</>
I have seen some beginners prefer the latter when starting to code in Reason probably because they are used to obj || {name: ''}
in JS but the first scales much better so you don’t have to change a default value if the shape of your data changes