Introduction
React is an open source JavaScript library for building user interfaces, often for single-page web applications. It is maintained by Facebook and a community of individual developers and companies.
Step 1: Installation
First of all, you need to have Node.js and npm (node package manager) installed on your computer. You can download them from the Node.js official site.
Once installed, you can create a new React project with the command:
npx create-react-app mio-progetto
Where mio-progetto
is the name of your project.
Step 2: Project structure
Navigate to your project directory with cd mio-progetto
and open the code in an editor of your choice. You will see a folder structure like this:
- node_modules/
- public/
- index.html
- src/
- App.js
- index.js
- package.json
React code typically goes in the src/
folder.
Step 3: Create a component
In React, everything is a component. A component is basically a part of the user interface. You can create a new component by creating a new file in the src/
folder. For example, src/MioComponente.js
.
In this file, you might have something like this:
import React from 'react';
class MioComponente extends React.Component {
render() {
return <h1>Ciao, mondo!</h1>;
}
}
export default MioComponente;
Then, you can use this component in other components by importing it.
Step 4: Rendering the component
To render your own component, you will need to add it to the src/App.js
file.
import React from 'react';
import MioComponente from './MioComponente';
class App extends React.Component {
render() {
return (
<div className="App">
<MioComponente />
</div>
);
}
}
export default App;
Step 5: Launch the application
Finally, to start the application, use the npm start
command in the root of your project. You should see the application launch and your component rendered in the browser.
Conclusion
React is a powerful library for building interactive user interfaces with ease. This tutorial is just the tip of the iceberg of what's possible with React. Happy programming!