Installation and Setup

suggest change

To use TypeScript with React in a Node project, initialize a project directory with npm init.

Installing via npm or yarn

You can install React using npm by doing the following:

npm install --save react react-dom

Facebook released its own package manager named Yarn, which can also be used to install React. After installing Yarn you just need to run this command:

yarn add react react-dom

You can then use React in your project in exactly the same way as if you had installed React via npm.

Installing React type definitions in TypeScript 2.0+

To compile your code with TypeScript, install the type definition packages with npm or Yarn.

npm install --save-dev @types/react @types/react-dom

or, using yarn

yarn add --dev @types/react @types/react-dom

Installing React type definitions in older versions of TypeScript

You have to use a separate package called tsd

tsd install react react-dom --save

Adding or Changing the Typescript configuration

To use JSX, which mixes JavaScript with HTML-like markup, change the TypeScript compiler configuration. In tsconfig.json, set the jsx option:

"compilerOptions": {"jsx": "react"},

That option tells the TypeScript compiler to translate JSX tags into JavaScript function calls.

To leave the JSX for another tool instead of emitting those calls, use:

"compilerOptions": {
    "jsx": "preserve"
},

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents