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.

Let’s discuss how components could look like in a future version of HyperApp.

Submitted Proposals


Example

counter.js

const Counter = (props, children) => ({
  state: {
    value: props.value
  },
  view: ({ value }, { inc, dec }) =>
    <div>
      <h1>{state.value}</h1>
      <button onclick={inc}>+</button>
      <button onclick={dec}>-</button>
    </div>,
  actions: {
    inc: state => state + 1,
    dec: state => state - 1
  }
})

index.js

app({
  state: {
    title: "Hello."
  },
  view: (state, actions) =>
    <main>
      <h1>{state.title}</h1>
      <Counter value={1} />
    </main>
})

Credits

All credit goes to @MatejMazur:

Related

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:171 (117 by maintainers)

github_iconTop GitHub Comments

10reactions
jorgebucarancommented, Jul 1, 2017

I am having mixed feelings about components and I am not even sure we should add a new core concept to the framework. I am not even sure we even have a problem. I say, let’s build one or two cool apps first and then revisit this issue. 🤔😅😎

Now, I’m still interested to see @naugtur’s #245’s final form and the door is open to anyone who wants to add more to this discussion.

I am glad this conversation happened, though, because it motivated me to revisit hyperapp core values and I can confidently say I like it now more than ever.

To summarize my feelings about components:

@zaceno’s But the patterns I’ve seen so far have state and actions that are defined very independently. Even though we’re merging their state and actions to the main tree, they don’t seem to have any access to the main state tree. So how are the components meant to communicate? If the answer is: “via the props”, then they are effectually no different to locally stateful components. And if that’s the case, then what’s the point of the “one-state-to-rule-them-all”?

But nothing is lost. On the contrary, we’ve gained the insight that there exist patterns that can help you create complex applications without losing the benefits of a single state tree.

Patterns

Widgets

@zaceno’s state/action bound custom tags is interesting and could be published as an independent module. I like to think of them as “widgets”. 🤓

See: https://codepen.io/zaceno/pen/gRvWPx

Split-mixins

@MatejMazur’s Split-mixins is a clever trick that requires no changes to core and lets you use mixins to achieve something similar to stateful components.

const MyComponent = ({ state, view, actions }) => view
   ? ({ state, actions, events, mixins }) // good ol' mixin
    : <main>...</main> // a custom tag

Mixins

@Swizz Super-powered mixins.

See: https://github.com/hyperapp/hyperapp/issues/238#issuecomment-310999839.

Other

Final remarks

We’re all (I am!) on a learning curve and tyring to figure this out. I am sure more patterns will show up, so please share your research and findings.

Thank you all, for your feedback and such an epic thread! 💥 🎉🥇

4reactions
jorgebucarancommented, Jun 28, 2017

@MatejMazur @Swizz @jamen @everyone

How does everyone feel about this approach?

app.js

import { h, app } from "hyperapp"
import MyCounter from "./counter"

app({
  state: {
    title: "Hello."
  },
  view: state =>
    <main>
      <h1>{state.title}</h1>
      <MyCounter initialValue={1} />
    </main>
})

counter.js

import { h, component } from "hyperapp"

export default component(({ initialValue }) => ({
  state: initialValue,
  view: (state, { up, down }) =>
    <div>
      <h1>{state}</h1>
      <button onclick={up}>+</button>
      <button onclick={down}>-</button>
    </div>,
  actions: {
    up: state => state + 1,
    down: state => state - 1
  }
}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Component Definition & Meaning - Merriam-Webster
The meaning of COMPONENT is a constituent part : ingredient. How to use component in a sentence. Synonym Discussion of Component.
Read more >
Component - Definition, Meaning & Synonyms - Vocabulary.com
Technically speaking, a component is an element of a system or a part of a machine. But a component can also be a...
Read more >
Component Definition & Meaning - Dictionary.com
being or serving as an element (in something larger); composing; constituent: the component parts of a computer system. QUIZ. WILL YOU SAIL OR...
Read more >
COMPONENT | definition in the Cambridge English Dictionary
one of the parts of a system, process, or machine: Fair pay for child-care providers is a vital component of welfare reform.
Read more >
41 Synonyms & Antonyms for COMPONENTS - Thesaurus.com
Find 41 ways to say COMPONENTS, along with antonyms, related words, and example sentences at Thesaurus.com, the world's most trusted free thesaurus.
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