Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After settings element text content, it won't redraw #2962

Closed
1 task done
up9cloud opened this issue Sep 15, 2024 · 1 comment
Closed
1 task done

After settings element text content, it won't redraw #2962

up9cloud opened this issue Sep 15, 2024 · 1 comment
Assignees
Labels
Area: Core For anything dealing with Mithril core itself Type: Gotcha For unwanted behavior that is still correct under the given circumstances

Comments

@up9cloud
Copy link

up9cloud commented Sep 15, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Mithril.js Version

2.2.2

Browser and OS

Chrome latest, Ubuntu 22.04

Project

No response

Code

import m from 'mithril'

const state   = { count: 0 }

const actions = {
  increment: () => state.count += 1,
  decrement: () => state.count -= 1
}

const Counter = {
  oninit() {
    document.getElementById('r1_1').textContent = state.count
  },
  view: () => {
    return (
      <div>
        <button onclick={actions.increment}>+</button>
        <button onclick={actions.decrement}>-</button>
        <h2>Not work:</h2>
        <div>textContent:<span id="r1_1">{state.count}</span></div>
        <h2>Control group:</h2>
        <div>{state.count}</div>
      </div>
    )
  }
}

m.mount(document.getElementById("app"), {
  view: () => <Counter></Counter>
})

Steps to Reproduce

See the code

Expected Behavior

It should redraw

Observed Behavior

It didn't redraw

Context

No response

@up9cloud up9cloud added the Area: Core For anything dealing with Mithril core itself label Sep 15, 2024
@dead-claudia
Copy link
Member

dead-claudia commented Sep 17, 2024

Mithril builds and modifies the inner text node directly as it's simpler. It doesn't call .textContent except when initially clearing a node it's about to mount into. Since .textContent replaces the text node, Mithril just retains a reference to the newly detached node.

It's best in general to either only use Mithril to add children to a node or only use another mechanism to add children to a node. Anything else is a recipe for disaster, either with Mithril or with whatever other library or system you're using.

@dead-claudia dead-claudia added the Type: Gotcha For unwanted behavior that is still correct under the given circumstances label Sep 17, 2024
@dead-claudia dead-claudia closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Core For anything dealing with Mithril core itself Type: Gotcha For unwanted behavior that is still correct under the given circumstances
Projects
None yet
Development

No branches or pull requests

2 participants