问题
I have an app where I want to control when to redraw the view.
I can make it work using m.mount and m.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>
But if i use m.render (because I don't need mithril to autoredraw) it no longer works:
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>
How can I make mithril redraw when using m.render instead of m.mount?
回答1:
As stated here in the mithril docs:
Note that
m.redrawonly works if you usedm.mountorm.route. If you rendered viam.render, you should usem.renderto 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>
来源:https://stackoverflow.com/questions/46215561/mithril-cannot-m-redraw-with-m-render