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.

multiple `app()`s / component approach / return dom

See original GitHub issue

So, it is just amazing lib. A was working on some idea like that, code name mitak ;d Based on events with just router, pub/sub system. For now it is 450bytes gzip. But yea, anyway.

So the problem of return nothing just bumped while I tried to make a Todo List in components way.

So lets get started. We have 3 app()s - TodoItem, TodoList and main.

  • main - main entry, place for header & footer for example
  • TodoItem - component for each todo, e.g. <li></li>
  • TodoList - component holding all TodoItems, actions and view which is just like <ul>TodoItem()</ul>

main

const { app, html } = require('hyperapp')

const main = app({
  // root: document.body,
  model: {
    title: 'Todo List Example',
    link: 'https://github.com/hyperapp/hyperapp'
  },
  view: (state) => html`<main id="HyperApp">
    <h1>${state.title}</h1>
    ${TodoList()}
    <footer>Powered by <a data-no-routing href="${state.link}">HyperApp</a></footer>
  </main>`
})

TodoList

const TodoList = () => app({
  model: {
    todos: [],
    count: 0,
    input: '',
    placeholder: 'Add new todo'
  },
  update: {
    toggle: (state, data) => ({
      todos: state.todos.map((item) => {
        return data.id === item.id
          ? Object.assign({}, item, { done: !data.done })
          : item
      })
    }),
    remove: (state, data) => ({
      todos: state.todos.map((todo) => todo.id !== data.id)
    }),
    add: (state) => ({
      todos: state.todos.concat({
        id: state.count++,
        done: false,
        text: state.input
      })
    }),
    input: (state, data) => ({ input: data.value })
  },
  view: (state, action) => html`<section>
    <ul>${state.todos.map((item) => TodoItem(item, action))}</ul>
    <p>
      <input
        class="add"
        type="text"
        onkeyup=${e => e.keyCode === 13 ? action.add() : ''}
        oninput=${e => action.input({ value: e.target.value })}
        placeholder=${state.placeholder}
      />
      <button onclick=${action.add}>add</button>
    </p>
  </section>`
})

TodoItem

const TodoItem = (state, actions) => app({
  model: state,
  update: actions,
  view: (item, action) => html`<li onclick=${e => action.toggle(item)}>${item.text}</li>`
})

All is okey, but it adds Todo to DOM body two times

2017-01-31-14 53 55_1280x1024_scrot

Passing opts.root seems to not deal with that problem.

The only thing that I changed to the core source code, is to return node from app function.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jorgebucarancommented, Feb 1, 2017

@tunnckoCore @tzellman @evgenykochetkov Here’s a TodoMVC implementation using HyperApp.

screen shot 2017-02-01 at 23 54 37
2reactions
evgenykochetkovcommented, Jan 31, 2017

Oh, sorry, I was not aware of discussion on reddit.

edit: I think I found it. Very interesting, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use One Component to Render Many HTML ...
A typical React component is expected to return multiple JSX elements. ... This guide explores an easy approach to render many HTML elements ......
Read more >
Return multiple React elements in a method without a wrapper ...
I know the render method has to return exactly 1 React element. Using a helper method like this would trigger a warning, and...
Read more >
Understanding refs in Vue - LogRocket Blog
Refs are Vue.js instance properties used to register a reference to HTML elements or child elements in the template of your application.
Read more >
How To Handle Routing in React Apps with React Router
First, it follows the standard declaractive nature of React code. You don't need to add a lot of code in componentDidMount methods or...
Read more >
React Router v5: The Complete Guide - SitePoint
Inside the App component, we've written the logic for routing. The <Route> 's path is matched with the current location and a component...
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