How to import 3rd party React components to Typescript TSX files?
I have a pretty big JavaScript front-end project which uses:
- Typescript
- React (0.14.6)
- React-dom (0.14.6)
- RequireJS
I want to add two 3rd party React components:
- React-DatePicker (https://github.com/Hacker0x01/react-datepicker)
- React-Select (https://github.com/JedWatson/react-select)
To make things little clear, I have created a basic lightweight project that duplicates basic tools on the GitHub (https://github.com/mgrackerl/react-requirejs-example) where you can try your code.
About the sample project files:
- rconfig.js - RequireJS module configuration file.
- typings - this directory holds d.ts files.
- scripts/main.tsx - main JSX file where I should add 3rd party components
To use the DatePicker, what I want as final result is something like this:
import React = require('react');
import ReactDOM = require('react-dom');
import DatePicker = require('react-datepicker');
let handleChange = function(date: Date) {
this.setState({
startDate: date
});
};
var myDivElement = <div>
<DatePicker
selected={new Date()}
onChange={handleChange} />
</div>;
ReactDOM.render(myDivElement, document.getElementById('example'));