# Input field blinks before update in re-frame

**URL:** https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758
**Category:** Troubleshooting
**Tags:** re-frame, clojurescript
**Created:** [October 29, 2020, 9:25pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758 "2020-10-29T21:25:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![DrLjotsson](https://clojureverse.org/user_avatar/clojureverse.org/drljotsson/32/2602_2.png) [@DrLjotsson](https://clojureverse.org/u/DrLjotsson)
#### Post date: [October 29, 2020, 9:25pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/1 "2020-10-29T21:25:12Z")

</div>

I am building a re-frame application and use re-com DOM elements, among others the `re-com/input-text` element. The value in the text element is not pushed to the app-db until the input loses focus, which is the default behavior.

One really annoying thing when I have 10x and spec checking of the app-db turned on, meaning that it takes some time after the new value is dispatched until it is saved, is that the text in the input field reverts to its old value until it gets the new value back again from the subscription. That is, after leaving the input field, the old value is shown for a short time, and then the new value is shown again.

Basically, the code looks like this

```auto
(re-frame/reg-sub :test-sub
  (fn [db]
    (:test-sub db)))

(re-frame/reg-event-db :test-event
  (fn [db [_ value]]
    (assoc db :test-sub value)))

(defn main-panel []
  (let [test (re-frame/subscribe [:test-sub])]
    [re-com/v-box
     :height "100%"
     :children [[:span @test]
                [re-com/input-text
                 :model test
                 :on-change #(re-frame/dispatch [:test-event %])]]]))

```

Can I prevent this behavior somehow?

This is also noticeable, although the delay is much shorter, in the lightweight re-frame template app. I’ve made a quick demonstration of the phenomenon available at [https://github.com/brjann/re-frame-test](https://github.com/brjann/re-frame-test)

---

<div class="post-metadata">

### Author: ![tobyloxy](https://clojureverse.org/user_avatar/clojureverse.org/tobyloxy/32/2695_2.png) [@tobyloxy](https://clojureverse.org/u/tobyloxy)
#### Post date: [October 30, 2020, 7:24am UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/2 "2020-10-30T07:24:50Z")

</div>

On your computer is it visible at all on the re-com docs? I can’t see the effect.

> **[re-com Demo](https://re-com.day8.com.au/#/input-text)**

If you weren’t using re-com I’d suggest using an uncontrolled textarea, where you subscribe :default-value of the textarea to test-sub instead of subscribing :value to test-sub. Then you can control exactly when the textarea will re-render by updating the key metadata. Rough example using Reagent, but same thing applies in Re-frame:

```
;; A way to force an uncontrolled textfield to re-render is to change the key
(defn uncontrolled-with-key
[text]
[:form {:no-validate true
        :auto-complete "off"}
^{:key @!key}
[textfield {:id "uncontrolled-inner"
            :multiline true
            :style {:width 300
                    :font-size 1}
            :variant "outlined"
            :label "Uncontrolled"
            :default-value @!text}]])

(comment
; Resetting the text atom won't make the textfield re-render
(reset! !text "Chocolates")
; But changing the key will
(swap! !key inc)
:pass)
```

---

<div class="post-metadata">

### Author: ![DrLjotsson](https://clojureverse.org/user_avatar/clojureverse.org/drljotsson/32/2602_2.png) [@DrLjotsson](https://clojureverse.org/u/DrLjotsson)
#### Post date: [October 30, 2020, 8:52am UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/3 "2020-10-30T08:52:28Z")

</div>

Thanks @tobyloxy

The re-com demo page doesn’t seem to use re-frame, so the effect does not appear there.

I would prefer to use regular input fields for this input if possible. Maybe `:default-value` can be used on that element as well. I’ll try!

---

<div class="post-metadata">

### Author: ![DrLjotsson](https://clojureverse.org/user_avatar/clojureverse.org/drljotsson/32/2602_2.png) [@DrLjotsson](https://clojureverse.org/u/DrLjotsson)
#### Post date: [October 30, 2020, 12:33pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/4 "2020-10-30T12:33:22Z")

</div>

I’ve submitted an issue in re-com’s github repo [https://github.com/day8/re-com/issues/219](https://github.com/day8/re-com/issues/219)

---

<div class="post-metadata">

### Author: ![greinseth](https://clojureverse.org/user_avatar/clojureverse.org/greinseth/32/1815_2.png) [@greinseth](https://clojureverse.org/u/greinseth)
#### Post date: [October 30, 2020, 4:19pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/5 "2020-10-30T16:19:20Z")

</div>

You might want to use `re-frame/dispatch-sync` instead of the (deferred) `re-frame/dispatch`.

---

<div class="post-metadata">

### Author: ![LucyWang000](https://clojureverse.org/user_avatar/clojureverse.org/lucywang000/32/3004_2.png) [@LucyWang000](https://clojureverse.org/u/LucyWang000)
#### Post date: [October 31, 2020, 2:11am UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/6 "2020-10-31T02:11:11Z")

</div>

And call `reagent.core/flush` after that, otherwise the view re-render maybe still be delayed

---

<div class="post-metadata">

### Author: ![greinseth](https://clojureverse.org/user_avatar/clojureverse.org/greinseth/32/1815_2.png) [@greinseth](https://clojureverse.org/u/greinseth)
#### Post date: [October 31, 2020, 1:41pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/7 "2020-10-31T13:41:33Z")

</div>

I was not aware of `reagent.core/flush`. Good to know, thanks!

---

<div class="post-metadata">

### Author: ![DrLjotsson](https://clojureverse.org/user_avatar/clojureverse.org/drljotsson/32/2602_2.png) [@DrLjotsson](https://clojureverse.org/u/DrLjotsson)
#### Post date: [November 2, 2020, 8:16am UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/8 "2020-11-02T08:16:02Z")

</div>

Thanks @greinseth and @LucyWang000. `dispatch-sync` did the job. But it feels kind of hacky, I suspect that there is something not right with the re-com input component? If I see further delays, I’ll try to add `reagent-core/flush` as well.

Thanks again!

---

<div class="post-metadata">

### Author: ![prook](https://clojureverse.org/letter_avatar_proxy/v4/letter/p/e480ec/32.png) [@prook](https://clojureverse.org/u/prook)
#### Post date: [November 18, 2020, 3:06pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/9 "2020-11-18T15:06:58Z")

</div>

Hi. To anyone researching this issue, let me add my 2c (2 comments):

1. `dispatch-sync` is indeed hacky, don’t do it.
2. I think the blink is actually a re-com bug. I’ve analyzed the issue in detail here: [https://github.com/day8/re-com/issues/219#issuecomment-729728022](https://github.com/day8/re-com/issues/219#issuecomment-729728022)

---

<div class="post-metadata">

### Author: ![DrLjotsson](https://clojureverse.org/user_avatar/clojureverse.org/drljotsson/32/2602_2.png) [@DrLjotsson](https://clojureverse.org/u/DrLjotsson)
#### Post date: [February 9, 2021, 8:50pm UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/10 "2021-02-09T20:50:45Z")

</div>

This has been fixed in re-com 2.12.0

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [August 11, 2021, 8:50am UTC](https://clojureverse.org/t/input-field-blinks-before-update-in-re-frame/6758/11 "2021-08-11T08:50:48Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
