multiple `app()`s / component approach / return dom
See original GitHub issueSo, 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
TodoItem
s, 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
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:
- Created 7 years ago
- Reactions:1
- Comments:10 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@tunnckoCore @tzellman @evgenykochetkov Here’s a TodoMVC implementation using HyperApp.
Oh, sorry, I was not aware of discussion on reddit.
edit: I think I found it. Very interesting, thank you!