React JS is a popular JavaScript library for building user interfaces. It was developed by Facebook and is widely used in building single-page applications. In this tutorial, we will go through the basics of React JS and build our first application.
- Setting Up the Environment
Before we start building our React application, we need to set up the environment. Make sure you have Node.js installed on your system as we will be using npm (Node Package Manager) to install the necessary packages.
First, let’s create a new directory for our project and navigate into it:
mkdir react-tutorial
cd react-tutorial
Next, initialize a new Node.js project by running the following command:
npm init -y
Now, let’s install React and ReactDOM packages using npm:
npm install react react-dom
- Creating a Basic React Component
Let’s create our first React component. Create a new file namedApp.js
in the root directory of your project. In this file, we will define a simple functional component that displays a greeting message:import React from 'react';
function App() {
return (
Hello, React!
);
}
export default App;
3. Rendering the Component
Now, let's create an entry point for our React application. Create a new file named `index.js` in the root directory of your project. In this file, we will import our `App` component and render it to the DOM:
```jsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
-
Setting Up the HTML File
Create a new file namedindex.html
in the root directory of your project. Add the following code to it:<!DOCTYPE html> <html> <head> <title>React Tutorial</title> </head> <body> <div id="root"></div> </body> <script src="index.js"></script> </html>
- Running the Application
To run our React application, we need to compile the JSX code using Babel. We can do this using webpack. First, install webpack and babel-loader:npm install webpack babel-loader @babel/core @babel/preset-env @babel/preset-react webpack-cli --save-dev
Next, create a webpack.config.js
file in the root directory of your project with the following configuration:
module.exports = {
entry: './index.js',
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
rules: [
{
test: /.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
}
]
}
};
Now, add the following script to your package.json
file to compile and bundle the code using webpack:
"scripts": {
"build": "webpack"
}
Run the following command to compile and bundle the code:
npm run build
Finally, open the index.html
file in a web browser to see your React application running. You should see the greeting message "Hello, React!" displayed on the page.
Congratulations! You have successfully built your first React application. This is just the beginning of your journey into React JS. There are many more concepts to learn and explore, such as state management, props, hooks, and more. Keep practicing and experimenting with React to become proficient in building dynamic and interactive web applications. Happy coding!
I finally feel like I understand React under the hood. Thank you
React is complicated and unintuitive. Vuejs is intuitive and easy to learn. I use Vue for my personal projects but had to learn React for work. I wish Vuejs gains more popularity so that we don't have hv to deal with React
He is using node 16 and we are at version 22. So the course is old buy still fundamental
i have this 'render' is deprecated.ts(6385)
index.d.ts(37, 4): The declaration was marked as deprecated here.
It says ReactDOM.render() is deprecated. What should i do now
VuE RuLeZ
ReactDOM.render is deprecated, no?
Nice video Excelent Tutorial.
Sir we want more tutorials on NEXT JS because in future demand ❤
Looks kool
Awesome 👍🏼👍🏼👍🏼
❤❤
Jeremy McPeak again!!
Great
His command English is top notch
Yeah react 🤗
I Commented ❤️🖤
Great
Yay🎉
Please make it extremely beginner-friendly and I hope it's 5-6 hours long. Thank you so much for your service to the community.
Make it rain now❤😂🎉