Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component Modularity #22

Closed
roti opened this issue May 31, 2014 · 5 comments
Closed

Component Modularity #22

roti opened this issue May 31, 2014 · 5 comments

Comments

@roti
Copy link

roti commented May 31, 2014

Hi,

This is a question, not an issue. Regarding following statement:

This does, somewhat, negate the concept of component modularity; Quiescent's contention is that the benefit of top-down, value-based rendering exceeds that of truly modular components.

How can one achieve reuse if components are not modular (and therefore reusable)? I'm thinking of widgets, for example.

@Raynos
Copy link

Raynos commented May 31, 2014

You can create modular components if you have the correct data structures.

I do this pattern a lot

(q/defcomponent MyApp
  ""
  [state]
  (d/div (my-widget/render (:my-widget state))))

(def my-data {
  :my-widget (my-widget/state ...)
})

(q/render (MyApp my-data)
  (.getElementById js/document "root-element"))

Here a widget / component is split up into two functions, one that returns state and one that does rendering.

All you have to do is embed a components state on your state atom and then call the components rendering function with its state.

If you want to allow the component to mutate it's own state consider using something like om's cursors.

Another trick I do is I store channels on a components state and have the rendering logic put things into the channels and have the components state logic read from the channels and update it's own state.

If I ever want the parent to do something with the child component, I never mutate the child's state directly, instead I do

(my-widget/some-op (:my-widget state) ...)

@roti
Copy link
Author

roti commented Jun 6, 2014

I understand what you suggest (I actually do it a little different: each widget state is accessible in the atom by its id, so I always pass the entire atom around), but isn't this a way to build modular components on top of Quiescent? I am trying to understand the opinion quoted above, especially how one can reuse parts without relying on modular components.

@Raynos
Copy link

Raynos commented Jun 6, 2014

@roti I think the confusion here is

truly modular components.

Most people think you need local state to achieve "truly modular". I think this more about not using local state then being modular.

@roti
Copy link
Author

roti commented Jun 10, 2014

Could you elaborate on that? For example: how can I make a widget (e.g. datepicker) without local state? Or if widgets are not the answer, how can I organize my code, so I don't have to build the datepicker twice?

@Raynos
Copy link

Raynos commented Jun 10, 2014

@roti

You can implement the logic for a datepicker in one place. You can embed the state of a datepicker instance (one for each date picker you want) somewhere in your state atom.

You can then call render date picker with the instance state.

The important part is that you have modularity & encapsulation, the datepicker instance can have its own methods / update logic / event handling logic / etc. it's state is effectively owned by the datepicker.

However there is a difference between local state and state owned by one component. The latter is just by convenient rather then enforced by hiding state.

@levand levand closed this as completed Mar 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants