DEV Community

Cover image for What Westworld and React Hooks have in common
Julian Garamendy
Julian Garamendy

Posted on • Updated on

What Westworld and React Hooks have in common

⚠️ Spoiler alert! If you haven't watched Westworld perhaps you may want to skip this post.

In the first season of Westworld androids are reset and their memories erased every time they "die" or after each "narrative cycle". Androids cannot take their memories with them (nor learn from them) for the next cycle. In a way they're stateless.

Maeve

On episode 4 Maeve seems to malfunction and has memories from when the scientists fix her wounds and reset her every time. But this only lasts for one cycle and then she forgets again.

One day, after a daydreaming episode she draws her vision (a man in a hazmat suit) on a piece of paper and starts wondering where to keep the drawing. She suspects everything in her room is carefully reset every time.

Maeve lifts the floorboard

She decides to hide the piece of paper under a loose floorboard. When she lifts the board she finds a lot of similar drawings that had been stored in there before.

Maeve finds other drawings

That's basically React.useState.

Imagine a component with a text input. The user enters some data, but this is a pure function! where do we keep this data for the next time the function is run?

Under a floorboard.

const [drawings, setDrawings] = useFloorboard()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)