Previously in part 2 I RE-WROTE some simple Razor as a React JSX script. And here is the same JSX script in Typescript.
class RequisitionBox extends React.Component<IRequisitionProps,any> {
constructor(props: any) {
super();
this.state = { requistionData: props.requistionData };
}
render() {
var requisitionNodes = this.state.requistionData.map(requisition => <RequisitionItem Id={requisition.Id} DateCreated={requisition.DateCreated} />);
return (
<div>
<h4>Requisitions</h4>
<div>{requisitionNodes}</div>
</div>
);
}
}
class RequisitionItem extends React.Component<IRequistionItem, any> {
render() {
return (
<div>
<div> Requistion: {this.props.Id}</div>
<div> Name: {this.props.DateCreated}</div>
</div>
);
}
}
Yes its almost identical ! The main differences are that we no longer use Reacts getInitialState but rather set the initial state in the constructor which I think is … well it doesn’t matter that’s just how you do it in typescript. Also it uses generics.
I also created two interfaces (which have no meaning in javascript and are therefore not trans-piled into the outputted js).
interface IRequisitionProps {
requistionData: string
}
interface IRequistionItem {
Id: number;
DateCreated: any;
}
This is obviously extra code that you would not have to write in plain JSX. But the advantages of using typescript are now you have type safety, intellisense and debugging as the code renders.
Using http://json2ts.com/ you can automatically create your interface to help mimic your server side model on the client. So by supplying the json model emitted by server side controller
{"Id":2011055,"DateCreated":null}
We got an auto generated IRequisitionItem interface – not a big deal in such a trivial example but will be a time save on more complex models.
As a typescript novice the biggest issue I found in converting from JSX to TSX was to overthink the conversion process. Its important to to remember the actual code is almost exactly the same, but with seat belts on, after all it transpiles to the same javascript.
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var RequisitionBox = (function (_super) {
__extends(RequisitionBox, _super);
function RequisitionBox(props) {
_super.call(this);
this.state = { requistionData: props.requistionData };
}
RequisitionBox.prototype.render = function () {
var requisitionNodes = this.state.requistionData.map(function (requisition) {
return React.createElement(RequisitionItem, {"Id": requisition.Id, "DateCreated": requisition.DateCreated});
});
return (React.createElement("div", {"className": "requisition"}, React.createElement("h1", null, "Comments"), React.createElement("div", {"className": "requisitionList"}, requisitionNodes)));
};
return RequisitionBox;
})(React.Component);
var RequisitionItem = (function (_super) {
__extends(RequisitionItem, _super);
function RequisitionItem() {
_super.apply(this, arguments);
}
RequisitionItem.prototype.render = function () {
return (React.createElement("div", null, React.createElement("div", {"className": "col-md-6 "}, " Requistion: ", this.props.Id), React.createElement("div", {"className": "col-md-6 "}, " Name: ", this.props.DateCreated)));
};
return RequisitionItem;
})(React.Component);