秘银:不能 m.redraw 和 m.render

Mithril: cannot m.redraw with m.render

我有一个应用程序,我想在其中控制何时重绘视图。

我可以使用 m.mountm.redraw:

var count = 0;

var Counter = {
    view: function() {
        return m('main', [
            m('h1', ('Count: ' + count))
        ])
    }
}

m.mount(document.body, Counter);

window.setInterval(function () {
    count++;
    m.redraw();
}, 200);
<html>
<body>
    <script src="https://unpkg.com/mithril/mithril.js"></script>
    <script src="index.js"></script>
</body>
</html>

但是如果我使用 m.render(因为我不需要秘银来自动重绘)它就不再有效了:

var count = 0;

var Counter = {
    view: function() {
        return m('main', [
            m('h1', ('Count: ' + count))
        ])
    }
}

m.render(document.body, m(Counter)); // <-- The only changed line

window.setInterval(function () {
    count++;
    m.redraw();
}, 200);
<html>
<body>
    <script src="https://unpkg.com/mithril/mithril.js"></script>
    <script src="index.js"></script>
</body>
</html>

当使用 m.render 而不是 m.mount 时,如何重绘秘银?

如秘银文档中here所述:

Note that m.redraw only works if you used m.mount or m.route. If you rendered via m.render, you should use m.render to redraw.

var count = 0;

var Counter = {
    view: function() {
        return m('main', [
            m('h1', ('Count: ' + count))
        ])
    }
}

m.render(document.body, m(Counter));

window.setInterval(function () {
    count++;
    m.render(document.body, m(Counter)); // <-- Use m.render here, not m.redraw
}, 200);
<html>
<body>
    <script src="https://unpkg.com/mithril/mithril.js"></script>
    <script src="index.js"></script>
</body>
</html>