Typescript + React Setup
Typescript and React setup steps:
1. npm init
2. npm install typescript -g (if it was not already installed globally)
3. npm install typescript (ts-loader needs a local version of typescript installed in the project)
4. tsc --init
5. Example tsconfig.json
{
"compilerOptions": {
"target": "es5",
"jsx": "react", // Important
}
}
6. npm install --save react react-dom @types/react @types/react-dom
7. npm install --save-dev ts-loader webpack webpack-cli
8. Sample webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.tsx',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [ ".tsx", ".ts", ".js" ]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
9. Add script to package.json:
"build": "webpack"
10. Sample index.tsx file:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Hello } from './components/Hello';
ReactDOM.render(<Hello name="Anand" age={34} />, document.getElementById('app'));
11. Sample Hello.tsx file:
import * as React from 'react';
interface HelloProps {
name: string;
age: number;
}
export class Hello extends React.Component<HelloProps, {}> {
render() {
return (
<div>Hello there! You are {this.props.name} and you are {this.props.age} years old!</div>
);
}
}
12. Sample index.html file:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ReactJS + TypeScript</title>
</head>
<body>
<div id="app"></div>
<script src="./dist/bundle.js"></script>
</body>
</html>
1. npm init
2. npm install typescript -g (if it was not already installed globally)
3. npm install typescript (ts-loader needs a local version of typescript installed in the project)
4. tsc --init
5. Example tsconfig.json
{
"compilerOptions": {
"target": "es5",
"jsx": "react", // Important
}
}
6. npm install --save react react-dom @types/react @types/react-dom
7. npm install --save-dev ts-loader webpack webpack-cli
8. Sample webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.tsx',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [ ".tsx", ".ts", ".js" ]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
9. Add script to package.json:
"build": "webpack"
10. Sample index.tsx file:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Hello } from './components/Hello';
ReactDOM.render(<Hello name="Anand" age={34} />, document.getElementById('app'));
11. Sample Hello.tsx file:
import * as React from 'react';
interface HelloProps {
name: string;
age: number;
}
export class Hello extends React.Component<HelloProps, {}> {
render() {
return (
<div>Hello there! You are {this.props.name} and you are {this.props.age} years old!</div>
);
}
}
12. Sample index.html file:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ReactJS + TypeScript</title>
</head>
<body>
<div id="app"></div>
<script src="./dist/bundle.js"></script>
</body>
</html>
Great insights on setting up React efficiently! The step-by-step guide makes it easier for beginners to start building powerful web applications. For businesses aiming to create scalable and interactive interfaces, it’s wise to Hire ReactJS Developers who can deliver optimized and high-performing solutions.
ReplyDelete