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.

contentedtiable elements.

See original GitHub issue
const choo = require('choo')
const app = choo()

app.model({
  state: { title: 'Set the title' },
  reducers: {
    update: (action, state) => ({ title: action.value })
  }
})

const mainView = (params, state, send) => choo.view`
  <main>
    <h1>${state.title}</h1>
    <div
      contenteditable="true"
      oninput=${(e) => send('update', { value: e.target.value })}>Type here</div>
  </main>
`

app.router((route) => [
  route('/', mainView)
])

const tree = app.start()
document.body.appendChild(tree)

Doesn’t update the state, missing something?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
toddselfcommented, Jun 22, 2016

This works:

const choo = require('choo')
const app = choo()

app.model({
  state: { title: 'Set the title' },
  reducers: {
    update: (action, state) => ({ title: action.value })
  }
})

const mainView = (params, state, send) => choo.view`
  <main>
    <h1>${state.title}</h1>
    <div
      contenteditable="true"
      oninput=${(e) => send('update', { value: e.target.innerText })}>${state.title}</div>
  </main>
`

app.router((route) => [
  route('/', mainView)
])

const tree = app.start()
document.body.appendChild(tree)

The reason is that you’re sending the value of the <div> but not changing it. Every call to oninput causes the DOM to update based on the current value of the state.

So you delete the type here and it sends an oninput with e.target.innerText equal to '', and updates the state. Then it re-renders. But you haven’t changed what the input value for the contenteditable is, so it’s still type here when you go to type in it.

You delete, it sends, resets to type here, rerenders, etc.

You need to track the state in the element which is sending the updates as well since the state is the source of truth, not the DOM.

1reaction
timwiscommented, Jun 22, 2016

<div>s don’t have a value property - your looking for innerText. Here’s an example of a content editable div in a choo app.

Read more comments on GitHub >

github_iconTop Results From Across the Web

contenteditable - HTML: HyperText Markup Language | MDN
Chrome Edge contenteditable Full support. ChromeYes. Toggle history Full suppo... contenteditable="caret". Experimental Full support. ChromeYes. Toggle history Full suppo... contenteditable="events". Experimental Full support. ChromeYes. Toggle history...
Read more >
HTML DOM Element contentEditable Property
The contentEditable property sets or returns if the content of an element is editable. See Also. The isContentEditable Property · The HTML contenteditable...
Read more >
HTML contenteditable Attribute
The HTML contenteditable attribute is used to specify whether or not the content of an element is editable. You can use this attribute...
Read more >
How to make your content editable in HTML
To edit the content in HTML, we will use contenteditable attribute. The contenteditable is used to specify whether the element's content is ...
Read more >
HTML contenteditable Attribute
The contenteditable attribute in HTML is used to set whether the content is editable or not using boolean values true or false. This...
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