Mithril: cannot m.redraw with m.render

你说的曾经没有我的故事 提交于 2019-12-12 04:41:33

问题


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.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>


来源:https://stackoverflow.com/questions/46215561/mithril-cannot-m-redraw-with-m-render

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!