# Using setState with function in haxe-react

**URL:** https://community.haxe.org/t/using-setstate-with-function-in-haxe-react/303
**Category:** Haxe
**Tags:** react
**Created:** [December 23, 2017, 1:46am UTC](https://community.haxe.org/t/using-setstate-with-function-in-haxe-react/303 "2017-12-23T01:46:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RafaelOliveira](https://community.haxe.org/user_avatar/community.haxe.org/rafaeloliveira/32/67_2.png) [@RafaelOliveira](https://community.haxe.org/u/RafaelOliveira)
#### Post date: [December 23, 2017, 1:46am UTC](https://community.haxe.org/t/using-setstate-with-function-in-haxe-react/303/1 "2017-12-23T01:46:11Z")

</div>

Hi, I’m trying to use setState with a function with the previous state. My code is this:

```haxe
setState(function(prevState:AppState, props:Dynamic) {
    return { timeLeft: prevState.timeLeft - 1 };
});

```

this is my state:

```haxe
typedef AppState = {
 	timeToGo:DateTime,
    timeLeft:Int,
    timeLeftColor:String,
    btStartText:String,
    statusBar:String
}

```

but I’m receiving this error:

src/view/App.hx:118: lines 118-120 : prevState : view.AppState → props : Dynamic → { timeLeft : Int } should be haxe.extern.EitherType\<view.AppState → { ?timeToGo : Null\<datetime.DateTime\>, ?timeLeftColor : Null, ?timeLeft : Null, ?statusBar : Null, ?btStartText : Null }, haxe.extern.EitherType\<view.AppState → Dynamic → { ?timeToGo : Null\<datetime.DateTime\>, ?timeLeftColor : Null, ?timeLeft : Null, ?statusBar : Null, ?btStartText : Null }, { ?timeToGo : Null\<datetime.DateTime\>, ?timeLeftColor : Null, ?timeLeft : Null,?statusBar : Null, ?btStartText : Null }\>\>  
src/view/App.hx:118: lines 118-120 : For function argument ‘nextState’  
/usr/share/haxe/std/haxe/macro/Context.hx:272: characters 9-46 : Invalid input value  
/haxelib/react/1,3,0/src/lib/react/ReactMacro.hx:147: characters 11-36 : Called from  
/haxelib/react/1,3,0/src/lib/react/ReactMacro.hx:91: characters 16-45 : Called from  
/haxelib/react/1,3,0/src/lib/react/ReactMacro.hx:100: characters 47-71 : Called from  
/haxelib/react/1,3,0/src/lib/react/ReactMacro.hx:62: characters 13-35 : Called from  
/haxelib/react/1,3,0/src/lib/react/ReactMacro.hx:27: characters 10-54 : Called from  
src/view/App.hx:166: lines 166-175 : Called from

My code seems to be inside the EitherType list (view.AppState → Dynamic → { ?timeToGo : Null\<datetime.DateTime\>, ?timeLeftColor : Null, ?timeLeft : Null, ?statusBar : Null, ?btStartText : Null }) but I not used to write code with EitherType so maybe I’m missing something.

---

<div class="post-metadata">

### Author: ![elsassph](https://community.haxe.org/user_avatar/community.haxe.org/elsassph/32/60_2.png) [@elsassph](https://community.haxe.org/u/elsassph)
#### Post date: [December 24, 2017, 12:28am UTC](https://community.haxe.org/t/using-setstate-with-function-in-haxe-react/303/2 "2017-12-24T00:28:23Z")

</div>

Right, there is a unification issue or something.

You are returning only a partial state, which is totally allowed but not correctly recognized.

Solution is one of those options:

```haxe
setState(function(prevState:AppState, props:Dynamic) {
    return cast { timeLeft: prevState.timeLeft - 1 }; // make it dynamic
});

setState(function(prevState:AppState, props:Dynamic): react.Partial<AppState> {
    return { timeLeft: prevState.timeLeft - 1 };
});

```

---

<div class="post-metadata">

### Author: ![RafaelOliveira](https://community.haxe.org/user_avatar/community.haxe.org/rafaeloliveira/32/67_2.png) [@RafaelOliveira](https://community.haxe.org/u/RafaelOliveira)
#### Post date: [December 25, 2017, 10:01pm UTC](https://community.haxe.org/t/using-setstate-with-function-in-haxe-react/303/3 "2017-12-25T22:01:48Z")

</div>

Thanks, I tested the option with react.Partial and it worked.
