Skip to content

Commit

Permalink
Upgrade React to 0.14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
japgolly authored and xsistens committed Jan 20, 2016
1 parent 1c9298b commit 15f4d11
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions doc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,36 +412,18 @@ React Extensions
props.message)
```

* Sometimes you want to allow a function to both get and affect a portion of a component's state. Anywhere that you can call `.setState()` you can also call `zoom()` to return an object that has the same `.setState()`, `.modState()` methods but only operates on a subset of the total state.
* Sometimes you want to allow a function to both get and affect a portion of a component's state. Anywhere that you can call `.setState()` you can also call `focusState()` to return an object that has the same `.setState()`, `.modState()` methods but only operates on a subset of the total state.

```scala
def incrementCounter(s: CompState.Access[Int]): Callback =
def incrementCounter(s: CompStateFocus[Int]): Unit =
s.modState(_ + 1)

// Then in some other component:
case class State(name: String, counter: Int)

def render = {
val f = $.zoom(_.counter)((a,b) => a.copy(counter = b))
button(onclick --> incrementCounter(f), "+")
}
```

You can cut down on boilerplate by using [Monocle](https://github.com/julien-truffaut/Monocle)
and the [scalajs-react Monocle extensions](FP.md).
By doing so, the above snippet will look like this:

```scala
import monocle.macros._

@Lenses case class State(name: String, counter: Int)

def render = {
val f = $ zoomL State.counter
button(onclick --> incrementCounter(f), "+")
}
// Then later in a render() method
val f = $.focusState(_.counter)((a,b) => a.copy(counter = b))
button(onclick --> incrementCounter(f), "+")
```

*(Using the [Monocle extensions](FP.md) greatly improve this approach.)*

Differences from React proper
=============================
Expand Down Expand Up @@ -632,11 +614,8 @@ There are a number of conversions available to convert between `Callback` and `F
| `CallbackTo[A]` | `cb.toFuture` | `Future[A]` |
| `CallbackTo[Future[A]]` | `cb.toFlatFuture` | `Future[A]` |
| `=> Future[A]` | `CallbackTo(f)` | `CallbackTo[Future[A]]` |
| `=> Future[CallbackTo[A]]` | `CallbackTo.future(f)` | `CallbackTo[Future[A]]` |
| `=> Future[CallbackTo[A]]` | `Callback.future(f)` | `Callback` |

If you're looking for ways to block (eg. turning a `Callback[Future[A]]` into a `Callback[A]`),
it is not supported by Scala.JS (See [#1996](https://github.com/scala-js/scala-js/issues/1996)).
| `=> Future[CallbackTo[A]]` | `CallbackTo.future(f)` | `CallbackTo[Future[A]]` |

**NOTE:** It's important that when going from `Future` to `Callback`, you're aware of when the `Future` is instantiated.

Expand All @@ -651,7 +630,7 @@ Callback.future(updateComponent)

// This is BAD because the callback wraps a single instance of updateComponent.
// 1) The server will be contacted immediately instead of when the callback executes.
// 2) If the callback is executed more than once, the future and old result will be reused.
// 2) If the callback is execute more than once, the future and old result will be reused.
val f = updateComponent
Callback.future(f)

Expand All @@ -663,6 +642,9 @@ Callback.future {
}
```

If you're looking for ways to block (eg. turning a `Callback[Future[A]]` into a `Callback[A]`),
it is not supported by Scala.JS (See [#1996](https://github.com/scala-js/scala-js/issues/1996)).


Gotchas
=======
Expand Down

0 comments on commit 15f4d11

Please sign in to comment.