I’ve recently been reading through the book Road to React in order to learn more about React.
The first half of the book develops an example React app in JavaScript. Every chapter adds more features to the app and shows the reader more of the functionality of React.
Road to React was recently rewritten to be based on React Hooks.
I’ve translated the example from Road to React to PureScript using the purescript-react-basic-hooks library. If I was writing a larger application and not just a self-contained example, there are some things I would do differently, but I thought some people here might be interested anyway:
See the README.md in the repo for more info.
The translation from the JavaScript version to PureScript was very straight-forward (mostly thanks to how well react-basic-hooks
is written), but there was one thing I had some trouble with.
I wasn’t sure how to represent JavaScript’s ability to have default values for function arguments.
For example, take the following component in JavaScript:
You can see that this component takes some props as arguments. The prop type
has a default value of "text"
.
This is what I wasn’t sure how to express in PureScript.
I ended up using a type class to express a record with both optional and required values. I defined the defaults for the optional values in the function:
The InputProps
type class synonym is defined as so:
This isn’t the prettiest code, but it seems to work.
How do other people deal with this type of situation?