React is a powerful JavaScript library for building user interfaces, developed by Facebook. It simplifies the process of creating dynamic, interactive web applications. This guide will walk you through creating a React application from scratch.
Prerequisites
Before you start, ensure you have the following installed on your system:
-
Node.js and npm:
- Download and install Node.js (npm is included with Node.js).
- Verify installation:
node -v npm -v
Text Editor:
Use any code editor you choose, such as Visual Studio Code.
Step 1: Install create-react-app
create-react-app
is an official tool by the React team to quickly set up a new React project with a good default configuration.
- Open your terminal or command prompt and install
create-react-app
globally:
npm install -g create-react-app
Step 2: Create a New React App
- Navigate to Your Project Directory: Choose where you want your project to reside. For example:
cd Desktop
-
Create the React App:
Replace
todolist
with your desired project name:
npx create-react-app todolist
- This command creates a new folder (
todolist
) with all the necessary files and dependencies.
- Navigate into the Project Folder:
cd todolist
Step 3: Start the Development Server
Run the following command to launch the development server:
npm start
- Your default browser should open automatically, displaying the React app at http://localhost:3000.
- If it doesn’t, manually open your browser and go to that address.
Step 4: Explore the Project Structure
Here's an overview of the files and folders created by create-react-app
:
todolist/
├── node_modules/
├── public/
│ ├── index.html // Main HTML file
├── src/
│ ├── App.js // Main React component
│ ├── index.js // Entry point for the React app
│ └── App.css // Styling for the App component
├── .gitignore
├── package.json // Project metadata and dependencies
└── README.md
Step 5: Modify Your React App
-
Edit the Main Component (
App.js
): Opensrc/App.js
in your text editor and customize the JSX to change the content:
function App() {
return (
<div className="App">
<h1>Hello, React!</h1>
</div>
);
}
export default App;
- Save Changes and See Updates: After saving, the browser automatically refreshes to reflect your changes.
Conclusion
Congratulations! You've successfully created and run your first React app. This setup provides a solid foundation for building powerful, component-based web applications. Explore React components, state management, and hooks to enhance your skills further!
Top comments (0)