Skip to content

Conversation

Ahmad-Helmy
Copy link

@Ahmad-Helmy Ahmad-Helmy commented Aug 11, 2025

Description

m.reactive for reactive components

Wrape your component with m.reactive() and use m.redraw(component) from any place and it will just redraw this component and its children
eg:

var count = 0;

var Child = m.reactive({
    view: function () {
        return m("div", {}, [
            m("p", "Count: " + count),
            m(
                "button",
                {
                    onclick: (e) => {
                        e.redraw = false;
                        count++;
                        m.redraw(Child);
                    },
                },
                "increment"
            ),
        ]);
    },
});

var Parent = {
    view: function () {
        return m("div", {}, [
            m("h1", "Hello, Mithril! " + count),
            m(Child),
        ]);
    },
};

the child function will re-render, but parent function won't
NOTE:
you can use m.redraw() and it will work normally as expected just if you want re-render specific component add it as a param.

Motivation and Context

adding component level re render is improving performance

How Has This Been Tested?

until now manual tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My change requires a documentation update, and I've opened a pull request to update it already:
  • I have read https://mithril.js.org/contributing.html.

@Ahmad-Helmy Ahmad-Helmy requested a review from a team as a code owner August 11, 2025 16:27
@dead-claudia
Copy link
Member

I'd prefer to see https://github.com/dead-claudia/mithril-helpers/blob/master/docs/self-sufficient.md integrated instead, as that does similar scoping without also forcing things to be global. I do appreciate the effort, though.

Also, your PR is missing tests. (My utility linked above was only hand-tested, so even if you were to do mine instead, you'd still have to roll tests for it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants