Skip to content

Commit

Permalink
Animated gifs for react articles !
Browse files Browse the repository at this point in the history
  • Loading branch information
vbfox committed Feb 6, 2018
1 parent ee86413 commit 6107e26
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion _drafts/fable-react-1-react-in-fable-land.md
Expand Up @@ -261,7 +261,7 @@ let init() =
ReactDom.render(element, document.getElementById("root"))
```

![Counter = 42]({{"/assets/fable-react/state-counter.png" | relative_url}})
![Counter = 42]({{"/assets/fable-react/state-counter.gif" | relative_url}})

*Note: This sample uses a few react-friendly optimizations that will be the subject of the second post.*

Expand Down
26 changes: 17 additions & 9 deletions _drafts/fable-react-2-optimizing-react.md
Expand Up @@ -90,7 +90,9 @@ let init() =
ReactDom.render(counter (), document.getElementById("root"))
```

![PureComponent demo]({{"/assets/fable-react/pure-component.png" | relative_url}})
Before: ![Dead Canary]({{"/assets/fable-react/pure-component-dead.gif" | relative_url}})

After: ![Living Canary]({{"/assets/fable-react/pure-component-alive.gif" | relative_url}})

While our canary has no reason to update, each time the button is clicked it will actually
re-render. But as soon as we convert it to a `PureComponent` it's not updating anymore: None of it's props or state change so react doesn't event call `render()`.
Expand Down Expand Up @@ -148,7 +150,7 @@ let init() =
ReactDom.render(counter (), document.getElementById("root"))
```

![PureComponent demo]({{"/assets/fable-react/passing-functions.png" | relative_url}})
![Only the first canary live]({{"/assets/fable-react/passing-functions.gif" | relative_url}})

## Using `toArray`/`toList` and refs

Expand All @@ -159,7 +161,7 @@ allow for a very nice syntax, but it can be a performance problem and force usel
type [<Pojo>] CanaryProps = { name: string }
type Canary(initialProps) =
inherit PureComponent<CanaryProps, obj>(initialProps) // <-- Change to PureComponent here
inherit PureComponent<CanaryProps, obj>(initialProps)
let mutable x = 0
override this.render() =
x <- x + 1
Expand Down Expand Up @@ -195,17 +197,17 @@ let init() =
ReactDom.render(counter (), document.getElementById("root"))
```

![PureComponent demo]({{"/assets/fable-react/names-dead.png" | relative_url}})
![Only the last canary live]({{"/assets/fable-react/names-dead.gif" | relative_url}})

It seem that *Chantilly* survives but in fact it's an illusion, a new element is always created at the end with his name, and all others are mutated.

So let's fix it by assigning an unique key to all our canaries :
So let's fix it by exposing an array via `toList` and assigning an unique key to all our canaries :

```fsharp
type [<Pojo>] CanaryProps = { key: string; name: string }
type Canary(initialProps) =
inherit PureComponent<CanaryProps, obj>(initialProps) // <-- Change to PureComponent here
inherit PureComponent<CanaryProps, obj>(initialProps)
let mutable x = 0
override this.render() =
x <- x + 1
Expand Down Expand Up @@ -243,7 +245,7 @@ let init() =
ReactDom.render(counter (), document.getElementById("root"))
```

![PureComponent demo]({{"/assets/fable-react/names-ok.png" | relative_url}})
![Every canary live]({{"/assets/fable-react/names-ok.gif" | relative_url}})

We could have kept using `yield!` instead of using `ofList` and it would have worked here
with only the keys but it's better to always use `ofList`.
Expand Down Expand Up @@ -282,7 +284,11 @@ type Canary(initialProps) =
let inline canary () = ofType<Canary,_,_> createEmpty []
module WrapperModule =
let Wrapper(props: obj) = canary ()
let Wrapper(props: obj) =
div [] [
h3 [] [str "In module"]
canary ()
]
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Remove 'inline' here and the problem is solved, MAGIC !
Expand Down Expand Up @@ -314,7 +320,9 @@ let init() =
ReactDom.render(counter (), document.getElementById("root"))
```

![PureComponent demo]({{"/assets/fable-react/pure-component.png" | relative_url}})
Before: ![Dead canary]({{"/assets/fable-react/module-dead.gif" | relative_url}})

After: ![Living canary]({{"/assets/fable-react/module-alive.gif" | relative_url}})

## Letting React concatenate

Expand Down
Binary file added assets/fable-react/module-alive.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fable-react/module-dead.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fable-react/names-dead.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/fable-react/names-dead.png
Binary file not shown.
Binary file added assets/fable-react/names-ok.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/fable-react/names-ok.png
Binary file not shown.
Binary file added assets/fable-react/passing-functions.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/fable-react/passing-functions.png
Binary file not shown.
Binary file added assets/fable-react/pure-component-alive.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fable-react/pure-component-dead.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/fable-react/pure-component.png
Binary file not shown.
Binary file added assets/fable-react/state-counter.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/fable-react/state-counter.png
Binary file not shown.

0 comments on commit 6107e26

Please sign in to comment.