1

this is my ts code:

import AddToCartBtn from "./AddToCartBtn";
import * as React from "react";
import * as ReactDOM from "react-dom";

declare var document:Document;
var _handler : __React.MouseEventHandler;

ReactDOM.render(<AddToCartBtn clickHandler={_handler}></AddToCartBtn>, document.getElementById("bd"));

and compile to js like:

define(["require", "exports", "./AddToCartBtn", "react", "react-dom"], function (require, exports, AddToCartBtn_1, React, ReactDOM) {
    var _handler;
    ReactDOM.render(React.createElement(AddToCartBtn_1.default, {"clickHandler": _handler}), document.getElementById("bd"));
});

it is a define function and doesn't work. how do I get a require function?

2
  • can you post your tsconfig or the compiler options you use? Commented Nov 30, 2015 at 21:10
  • { "compilerOptions": { "target": "es5", "noImplicitAny": false, "module": "amd", "removeComments": false, "sourceMap": true, "jsx": "react" } } Commented Nov 30, 2015 at 22:10

3 Answers 3

1

You are compiling with --module (or -m) set to amd. You should probably use commonjs.

Sign up to request clarification or add additional context in comments.

Comments

0

The define function is expected. You need require defined in the global scope for it to work though. Depending on your setup, there are various ways of making this happen. I have always used the approach listed below:

Typically this could be done by importing require via a script tag in your html (given usage of react, I'm assuming you are targeting the browser). This assumes your ts file compiles to main.js. This example is directly from RequireJS docs

<script data-main="main" src="require.js"></script>

Another option is to call require in a different script tag following that:

<script src="require.js"></script>
<script>
    require(["main"]);
</script>

Comments

0

Just switch your tsconfig.json settings to this:

{ "compilerOptions": { "target": "es5", "noImplicitAny": false, "module": "commonjs", "removeComments": false, "sourceMap": true, "jsx": "react" } }

Then you will have set the module system to commonjs which is the default module system for typescript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.