cljsrn

https://github.com/drapanjanas/re-natal | https://github.com/drapanjanas/re-natal/wiki/FAQ | https://github.com/condense/mercury-app/wiki | https://github.com/seantempesta/expo-cljs-template/ https://www.npmjs.com/package/create-expo-cljs-app
pez 2020-11-06T15:25:53.153800Z

I’m having troubles getting a RN switch component to behave. When I switch it is briefly toggles back before toggling to the intended position again. I have a feeling it is because I am doing it the wrong way. This is the way I’m doing it (using re-frame):

(defn my-switch []
  (let [enabled? @(rf/subcribe [:enabled?])]
    [switch {:on-value-change #(rf/dispatch [:set-enabled (not enabled?)])
             :value           enabled?}]))

frankitox 2020-11-06T15:31:24.154900Z

Seems like a common performance problem in using reagent + re-frame. You could try using dispatch-sync + flush combo:

(defn my-switch []
  (let [enabled? @(rf/subcribe [:enabled?])]
    [switch {:on-value-change #(do (rf/dispatch-sync [:set-enabled (not enabled?)])
                                 (reagent.core/flush))
             :value           enabled?}]))

❤️ 1
pez 2020-11-06T15:32:44.155500Z

I’ll give that a shot!

pez 2020-11-06T15:34:52.157200Z

And yes, reagent is in the mix too. The problem started to be noticeable when I moved the switch state down a notch in the app-db structure. So it is quite deeply nested.

frankitox 2020-11-06T15:40:20.160Z

I don't see why that could be a problem, but I don't know enough about re-frame's internals. I usually have to sprinkle the code with those declarations, last time it happened when using react-native-draggable-flatlist. Sometimes I leave it as it is because the production build works a bit faster and the issue disappears there.

joshmiller 2020-11-06T16:22:55.161200Z

@pez You can also keep the enabled? state in a local ratom.

(let [enabled? (r/atom @(rf/subscribe [:enabled?])] ...

pez 2020-11-06T17:14:28.166300Z

Thanks! The sync-then-fush works great. With the local ratom I get a strange behaviour with it resetting on the first tap, then it starts to work. And, I think I see a slight performance difference. I have some content behind a (when enabled? …) and that content is more delayed with the ratom solution. The delay seems to be about as long as the glitch I had without these mitigations, so maybe it is really more “honest”, rather.