React Setup
React Setup:
1. npm init
2. npm i webpack webpack-cli --save-dev
3. Add a build script to package.json
"build": "webpack --mode production"
4. npm i @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev
5. Create a .babelrc and configure it:
{
"presets": [
"@babel/preset-env", --for compiling Javascript ES6 code down to ES5
"@babel/preset-react" --for compiling JSX and other stuff down to Javascript
]
}
6. Configure webpack.config.js:
const path = require('path');
module.exports = {
entry: './src/index.jsx',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: 'babel-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [ ".jsx", ".js" ]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
7. npm i react react-dom --save-dev
8. Sample index.jsx 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'));
9. Sample Hello.jsx file:
import * as React from 'react';
export class Hello extends React.Component {
render() {
return (
<div>Hello there! You are {this.props.name} and you are {this.props.age} years old!</div>
);
}
}
10. 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 i webpack webpack-cli --save-dev
3. Add a build script to package.json
"build": "webpack --mode production"
4. npm i @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev
5. Create a .babelrc and configure it:
{
"presets": [
"@babel/preset-env", --for compiling Javascript ES6 code down to ES5
"@babel/preset-react" --for compiling JSX and other stuff down to Javascript
]
}
6. Configure webpack.config.js:
const path = require('path');
module.exports = {
entry: './src/index.jsx',
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: 'babel-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [ ".jsx", ".js" ]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
7. npm i react react-dom --save-dev
8. Sample index.jsx 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'));
9. Sample Hello.jsx file:
import * as React from 'react';
export class Hello extends React.Component {
render() {
return (
<div>Hello there! You are {this.props.name} and you are {this.props.age} years old!</div>
);
}
}
10. 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