tmp dev
tmp dev

Reputation: 9193

mithril requires 'mithril' to be imported

I am writing a simple Mithril component, given below is the code

var m = require("mithril")

var MyComponent = {
    view() {
        return <button>Hello world!</button>;
    }
};

export default MyComponent;

My problem is that in this scenario I am not using m as required, however when I remove this the app does not run, I get the following error

Uncaught ReferenceError: m is not defined(…)

Upvotes: 2

Views: 681

Answers (1)

Tivac
Tivac

Reputation: 2573

It complains when you remove the m = require("mithril") line because when the JSX is transformed it becomes invocations of m().

var m = require("mithril")

var MyComponent = {
    view() {
        return <button>Hello world!</button>;
    }
};

export default MyComponent;

becomes

var m = require("mithril");

var MyComponent = {
    view: function view() {
        return m(
            "button",
            null,
            "Hello world!"
        );
    }
};

exports.default = MyComponent;

You can see the JSX transform live on the babel REPL

Upvotes: 9

Related Questions