Fork me on GitHub
#reagent
<
2017-04-04
>
mikerod15:04:07

I was experimenting with having local component state for a “toggle” sort of button. What I’m seeing though is that when I hit the “back” button on the browser, this state is reset each time.

mikerod15:04:27

I’m not positive I want to even have this sort of local state, but I was surprised by this behavior and was just trying to understand it.

mikerod15:04:03

I know this isn’t a lot to go on. I’m making a single page app. I’m just using url hashes and client-side routing to change the view content

mikerod15:04:44

so the back is just triggering a History change back to a former hash. I define local-state components like:

(defn local-state-component [] 
 (let [state-toggle (r/atom false)
        toggle-change #(swap! state-toggle not)]
   (fn [props] 
      <do things with the @state-toggle here>)))

mikerod15:04:42

my app is just something like:

(r/render (if (= :main (:active-view @app-state)) [local-state-component] [something-else]) (.getElementById js/document “app”))

mikerod15:04:30

So basically, when the :active-view changes (just a hash change in URL) from local-state-component, to something-else, back to local-state-component, the local state is reset to it’s initial value.

mikerod15:04:54

This might just be how it works. I think that may make sense...

pesterhazy15:04:01

if the component gets destroyed, its state is lost

pesterhazy15:04:44

if you want to preserve the state of the component, why use local state?

mikerod17:04:13

@pesterhazy I started to realize that after I asked this question

mikerod17:04:29

and I don’t actually think I want local state, I transitioned away from it. I was just making sure I understood the general problem.

mikerod17:04:57

Since this component is removed completely from the newly rendered (v)dom though, I guess it makes sense that when it comes back again, it is all new.

isaac_cambron22:04:23

I've wrapped a react component using adapt-react-class. The component itself has a very conservative componentWillReceiveProps(), which means that it doesn't actually end up rerendering when I change the reagent atoms I have it depend on. I think it's possible for me to add a watch to that ratom and use that to force an update, but I'm not sure how to hook that up. Any pointers?

isaac_cambron22:04:23

What I'm trying to make happen is for that reset! to rerender the component. It doesn't because the wrapped React component intentionally ignores changes to :defaultValue (as I understand it, anyway), so I need a hook to say "ok, but just recreate yourself anyway"

isaac_cambron22:04:37

and i am open to responses like "you are doing this all wrong"