question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make view easier to use in modules?

See original GitHub issue

I’m bringing #294 back from the dead!

Now that we have state slice and modules, I thought it was kinda awkward module views are hard to use.

Introducing views!

https://gist.github.com/Pyrolistical/f3a4b1577741dd847aef73300f8f11c3

28109185-04d043f4-66a3-11e7-8291-3a0f8f062b1c (I didn’t re-cap the demo, but you can run the new code yourself it works like this!)

I wrote a HoA withViews and added a views parameter to the view function. This allows us to write stuff like

withViews(app)({
  view(state, actions, {left, right}) {
    return <main>
      <left name="Left" />
      <right name="Right" />
      <p>Sum {state.left.count + state.right.count}</p>
    </main>
  },
  modules: {
    left: counter,
    right: counter,
  }
}, document.getElementById('app-entry'))

where counter is just a module

const counter = {
  state: {
    count: 0
  },
  view(state, actions, views) {
    return <div>
      <h1>{state.name} {state.count}</h1>
      <button onclick={actions.down} disabled={state.count <= 0}>-</button>
      <button onclick={actions.up}>+</button>
    </div>
  },
  actions: {
    down(state) {
      return {
        count: state.count - 1
      }
    },
    up(state) {
      return {
        count: state.count + 1
      }
    }
  }
}

I feel this is a missing piece to the modules story. Should we integrate this into the core or leave this as a HoA? You can see the source for withViews: https://gist.github.com/Pyrolistical/f3a4b1577741dd847aef73300f8f11c3#file-index-html-L83

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (18 by maintainers)

github_iconTop GitHub Comments

3reactions
zacenocommented, Oct 26, 2017

The way I see it, this sort of module views are basically the same as getters/computeds The only thing is that they return a vtree rather than a number or string. (And a “traditional” getter only needs access to the state, not actions, I suppose) My point being: rather than having support for something named “views” specifically, I’d rather have support for general getters, and we can use getters for “pre-wired views” as well as other things.

2reactions
Pyrolisticalcommented, Oct 27, 2017

@JorgeBucaran here are the goods, i appended to the gist.

I ran into #437 and #438, so I included #426 at the end of the gist to get around it.

(1) (easy) add a button to the main view that adds +1 to both left and right

both Just a new action:

addBoth(state, actions) {
  actions.bleft.up()
  actions.bright.up()
  // this or if you want a single update
  // return {
  //   bleft: {
  //     ...state.bleft, // not needed in this case, but generally is
  //     count: state.bleft.count + 1
  //   },
  //   bright: {
  //     ...state.bright, // not needed in this case, but generally is
  //     count: state.bright.count + 1
  //   }
  // }
}

(2) (hard) a button that adds a new counter, so no more right or left, but a collection of counters.

counters-everywhere This one was more tricky, I added a new many module, which takes in a module and allows you to add more

function many(module) {
  return {
    state: {
      substates: []
    },
    view(state, actions) {
      return state.substates.map((substate, i) => {
        const subactions = {}
        for (const action in module.actions) {
          subactions[action] = () => {
            const updatedSubstate = module.actions[action](substate)
            actions.update({
              substates: [...state.substates.slice(0, i), updatedSubstate, ...state.substates.slice(i + 1, state.substates.length)]
            })
          }
        }
        return module.view({
          ...substate,
          ...state.extraProps(i)
        }, subactions)
      })
    },
    actions: {
      push(state, actions) {
        return {
          substates: [...state.substates, {
            ...module.state
          }]
        }
      },
      update(state, actions, data) {
        return data
      }
    }
  }
}

usage looks like:

modules: {
  counters: many(counter)
}

the view looks like:

<h1>C. Counters, counters everywhere</h1>
<counters extraProps={(index) => ({name: `counter[${index}]`})} />
<button onclick={actions.counters.push}>Add Counter</button>
<p>Sum {state.counters.substates.reduce((total, {count}) => total + count, 0)}</p>

where counters is the from views

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a simple block view | Views module - Drupal
Views allows you to create listings of data on your site as blocks which can be placed in regions on your site.
Read more >
Filters and views in modules - IBM
Module views are a subset of the module content. It is a simple way to filter out content from the module and helps...
Read more >
Common modularization patterns - Android Developers
This page gives an overview of some general rules and common patterns that you can employ when developing multi module Android apps.
Read more >
Angular Modules Best Practices 2021 - Chris Kohler
How to decide what pattern to use? ... The easiest decision is to rule out putting everything into one single module. If the...
Read more >
Using Modules to Organize Course Content
Structuring your course with Modules can also make it easier for you to quickly see how students are progressing through the content and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found