Adding Side Effects with Storage
Let’s use this new and improved shirt order program as the base for our final example in this chapter. We’ll store the state of the page in the browser’s local storage[48] in JSON format.
We’ll need to include @glennsl/bs-json in our bsconfig.json and install it via npm install --save @glennsl/bs-json, as we did:
| "bs-dependencies": [ |
| "reason-react", |
| "@glennsl/bs-json" |
| ], |
We need to add code to convert to and from JSON. First, let’s look at the Order type in file Shirt.re. The new entries here are the encodeJson and decodeJson functions:
| module E = Json.Encode; |
| module D = Json.Decode; |
| |
| module Order = { |
| type t = { |
| orderNumber |
Get Web Development with ReasonML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.