Fork me on GitHub
#re-frame
<
2017-09-08
>
vinai16:09:44

Is it considered bad to use component local state in re-frame? Should everything go into the app-db?

vinai16:09:17

The use case I'm thinking about is a collapsible panel.

manutter5117:09:26

My (limited) experience has been that every time I think I’ve got a valid use case for local component state, I think of something I want to do with that data outside the local component.

manutter5117:09:56

Though that’s also led to the experience of finding it remarkably easy to refactor from local state to app-db state.

vinai17:09:53

Makes sense. If others have thoughts on that topic, I would be curious to hear more, too.

vinai17:09:14

I like managing state through events, so it feels like the intended way with re-frame is to use the app-db. However, it ends up a kitchen-sink 🙂

manutter5117:09:17

I think I did have a few use cases where local state made sense, like if you click on an “info” icon and it pops open a small text snippet in-line with the field it’s describing. Purely visual effect, no real mutable state to manage other than visibility on/off, so I went ahead and just used a local.

manutter5117:09:16

The way I manage the “exploding app-db” problem is by breaking it up into what I’ll call sub-db’s (for want of a better term). Like in app-db, I’ve got a key called :user-profile-editor that holds the data for editing a user profile form, any validation errors, and anything else I need for editing.

manutter5117:09:10

To edit a profile, I copy the data out of a different area of the app-db, and then when we’re done editing, copy it back. That way it’s easy to discard changes if the user clicks “Cancel”

manutter5117:09:03

But as far as the main app-db is concerned, all of that data is just an opaque block under the :user-profile-editor key, and all the code that manipulates that block is off in its own namespace.

vinai17:09:16

Thank you for your input @manutter51

vinai17:09:34

I'll try out that pattern.

mikethompson22:09:43

@vinai as a general rule, any state that can go in app-db should go in app-db. That's the enduring goal we should all be pursuing. Having said that, there are certainly cases, involving reusable components, where it makes sense for view-only-related state to be local.