10.05.2018 Views

Web_Designer_UK__May_2018

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Developer tutorials<br />

Deploy TypeScript with React<br />

Above Moving the body of the Node project to the same level as the webpack ‘config’ file fixes weird problems<br />

the source map directly.<br />

. . .<br />

/***/ })<br />

/******/ ]);<br />

//# sourceMappingURL=bundle.js.map<br />

18. Create a component<br />

One of TypeScript’s most important features is the use<br />

of classes and data types. Creating a new component<br />

is done with a structure similar to the one shown<br />

below – ‘Props’ and ‘State’ can be declared using the<br />

‘type’ keyword.<br />

type Props = { ... };<br />

type State = { ... };<br />

class MyComponent extends React.<br />

Component {<br />

...<br />

}<br />

19. Do it with interfaces<br />

Another way to solve the problem involves the use of<br />

interfaces. In this case, the syntax does not change<br />

significantly – Microsoft’s favourite example demos it<br />

via a set of string parameters passed in via the<br />

‘HelloProps’ object.<br />

export interface HelloProps { compiler:<br />

string; framework: string; }<br />

export class Hello extends React.<br />

Component {<br />

render() {<br />

return Hello from {this.props.<br />

compiler} and {this.props.framework}!;<br />

}<br />

}<br />

20. Deploy the component<br />

With that out of the way, the component can be<br />

integrated into our main window as if it were a normal<br />

React element.<br />

import { Hello } from “./components/Hello”;<br />

ReactDOM.render(<br />

,<br />

document.getElementById(“example”)<br />

);<br />

21. Use advanced features<br />

Simply introducing TypeScript to your code is not<br />

enough. Make sure to read the documentation carefully,<br />

as to enable your developers and yourself to use the<br />

most advanced features found in the language standard.<br />

22. Try it out online<br />

Should you ever feel like testing a bit of TypeScript, keep<br />

www.typescriptlang.org/play in mind. It contains a live<br />

version of the TypeScript compiler running in your<br />

browser. Simply enter code on the left-hand side, and see<br />

the JavaScript output on the right.<br />

23. Load some examples<br />

The TypeScript playground comes with a set of<br />

pre-loaded examples which can be loaded via the<br />

combination box on the left-hand side of your screen. In<br />

addition to various syntax samples, the product even<br />

contains an extremely interesting implementation of a<br />

ray tracer.<br />

90_________________________________________________tutorial

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!