ArneHugo
ArneHugo

Reputation: 6509

Mithril: cannot m.redraw with m.render

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?

Upvotes: 0

Views: 363

Answers (1)

ArneHugo
ArneHugo

Reputation: 6509

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>

Upvotes: 2

Related Questions