Hyperapp: Calling action in event handler leads to infinite loop

Created on 1 Oct 2018  路  4Comments  路  Source: jorgebucaran/hyperapp

I don't think that I'm posting in the right place, but resources are very limited for hyperapp.

I'm a beginner with this framework and I want to continue with it because I found that Vue, React, etc. are way too complex for my needs.

My app is an invoice. I created a table with cell that displays state values, but are also editable. The problem I ran into is an infinite loop. The state update the cell, the cell updates the state.

Here's my code:

jsx <td id="nb-hours" align="right" contenteditable="true" onfocusout={actions.updateNbHours(this.innerText)} > {state.nbHours} </td>

I'm sure that this is not the correct way to do it, any help?

V1

Most helpful comment

馃憢 Hi, @talelamira! It seems you forgot a () =>. V1 event handlers are functions receiving the event data, so you probably want to do as @zaceno suggested.

  • onfocusout ={actions.doSomething(withData)}
  • onfocusout ={() => actions.doSomething(withData)}

All 4 comments

Try this:

<td id="nb-hours" align="right" contenteditable="true" onfocusout={
         event => actions.updateNbHours(event.target.innerText)
       }>{state.nbHours}</td>

I'm just winging it, but I think that should work. At least you won't get an infinite loop :) The reason for your infinite loop is you are calling actions.updateNBHours(...) immediately when the node is calculated in the view. onfocusout should be a function (which receives the event object)

If you have other support-questions, the best place to go is to join the slack

We're very friendly :)

馃憢 Hi, @talelamira! It seems you forgot a () =>. V1 event handlers are functions receiving the event data, so you probably want to do as @zaceno suggested.

  • onfocusout ={actions.doSomething(withData)}
  • onfocusout ={() => actions.doSomething(withData)}

Thanks a lot. It works!
I'm not very slack, but I have a good reason now to open that forgotten app on my devices :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SkaterDad picture SkaterDad  路  3Comments

joshuahiggins picture joshuahiggins  路  4Comments

jorgebucaran picture jorgebucaran  路  3Comments

jamen picture jamen  路  4Comments

jacobtipp picture jacobtipp  路  3Comments