Skip to content
Permalink
Browse files

Fix setState crash when a component defines the state type as a Seq

  • Loading branch information...
shadaj committed Oct 9, 2019
1 parent aa93a74 commit 460a4f1f21a6ea389c13045bea388d5bbdeec56b
@@ -1,10 +1,13 @@
# Changelog

## vNEXT
### Highlights :tada:
+ Improve experience using `ReactElement`s within first-order types such as Map and List [PR #285](https://github.com/shadaj/slinky/pull/285)
+ Support [React Native's Keyboard API](https://facebook.github.io/react-native/docs/keyboard) [PR #293](https://github.com/shadaj/slinky/pull/293)

### Bug Fixes
+ Fix crashes when calling `setState` on a component with `type Props = Seq[...]` [PR #295](https://github.com/shadaj/slinky/pull/295)
+ Support `useCallback` with a function that takes arguments [PR #290](https://github.com/shadaj/slinky/pull/290)
+ Fix issues around using ReactElements within first-order types e.g. Map, List [PR #285](https://github.com/shadaj/slinky/pull/285)
+ Support [React Native's Keyboard API](https://facebook.github.io/react-native/docs/keyboard) via a scalajs facade [PR #293](https://github.com/shadaj/slinky/pull/293)

## [v0.6.2](https://slinky.dev)
### Highlights :tada:
@@ -57,7 +57,7 @@ abstract class DefinitionBase[Props, State, Snapshot](jsProps: js.Object) extend
readStateValue(this.asInstanceOf[PrivateComponentClass].stateR)
}

@JSName("setState_scala")
@JSName("setState_scala_1")
@inline final def setState(s: State): Unit = {
val stateObject = if (BaseComponentWrapper.scalaComponentWritingEnabled) {
writeWithWrappingAdjustment(stateWriter)(s)
@@ -66,7 +66,7 @@ abstract class DefinitionBase[Props, State, Snapshot](jsProps: js.Object) extend
this.asInstanceOf[PrivateComponentClass].setStateR(stateObject)
}

@JSName("setState_scala")
@JSName("setState_scala_2")
@inline final def setState(fn: State => State): Unit = {
this.asInstanceOf[PrivateComponentClass].setStateR((ps: js.Object) => {
val s = fn(readStateValue(ps))
@@ -76,7 +76,7 @@ abstract class DefinitionBase[Props, State, Snapshot](jsProps: js.Object) extend
})
}

@JSName("setState_scala")
@JSName("setState_scala_3")
@inline final def setState(fn: (State, Props) => State): Unit = {
this.asInstanceOf[PrivateComponentClass].setStateR((ps: js.Object, p: js.Object) => {
val s = fn(readStateValue(ps), readPropsValue(p))
@@ -86,15 +86,15 @@ abstract class DefinitionBase[Props, State, Snapshot](jsProps: js.Object) extend
})
}

@JSName("setState_scala")
@JSName("setState_scala_4")
@inline final def setState(s: State, callback: () => Unit): Unit = {
val stateObject = if (BaseComponentWrapper.scalaComponentWritingEnabled) {
writeWithWrappingAdjustment(stateWriter)(s)
} else js.Dynamic.literal(__ = s.asInstanceOf[js.Any])
this.asInstanceOf[PrivateComponentClass].setStateR(stateObject, callback)
}

@JSName("setState_scala")
@JSName("setState_scala_5")
@inline final def setState(fn: State => State, callback: () => Unit): Unit = {
this.asInstanceOf[PrivateComponentClass].setStateR((ps: js.Object) => {
val s = fn(readStateValue(ps))
@@ -104,7 +104,7 @@ abstract class DefinitionBase[Props, State, Snapshot](jsProps: js.Object) extend
}, callback)
}

@JSName("setState_scala")
@JSName("setState_scala_6")
@inline final def setState(fn: (State, Props) => State, callback: () => Unit): Unit = {
this.asInstanceOf[PrivateComponentClass].setStateR((ps: js.Object, p: js.Object) => {
val s = fn(readStateValue(ps), readPropsValue(p))
@@ -137,7 +137,6 @@ abstract class DefinitionBase[Props, State, Snapshot](jsProps: js.Object) extend

def componentDidCatch(error: js.Error, info: ErrorBoundaryInfo): Unit = {}

@JSName("render")
def render(): ReactElement

if (defaultBase != null) {
@@ -1 +1 @@
sbt.version=1.2.8
sbt.version=1.3.2
@@ -55,22 +55,22 @@ object TestComponentExtraApply extends ComponentWrapper {
}
}

object TestComponentForSetStateCallback extends ComponentWrapper {
type Props = Int => Unit
type State = Int
object TestComponentForSeqState extends ComponentWrapper {
type Props = () => Unit
type State = Seq[String]

class Def(jsProps: js.Object) extends Definition(jsProps) {
override def initialState: Int = 0
override def initialState = Seq.empty

override def componentDidMount(): Unit = {
setState((s, p) => {
s + 1
}, () => {
props.apply(state)
})
setState(state :+ "hello")
}

override def render(): ReactElement = {
override def render() = {
if (state.nonEmpty) {
props.apply()
}

null
}
}
@@ -319,6 +319,17 @@ class ComponentTest extends AsyncFunSuite {
promise.future
}

test("setState with Seq state runs correct overloaded definition") {
val promise: Promise[Assertion] = Promise()

ReactDOM.render(
TestComponentForSeqState(() => promise.success(assert(true))),
dom.document.createElement("div")
)

promise.future
}

test("Can construct a component and provide key") {
val element: ReactElement = TestComponent(_ => ()).withKey("test")
assert(element.asInstanceOf[js.Dynamic].key.toString == "test")

0 comments on commit 460a4f1

Please sign in to comment.
You can’t perform that action at this time.