React basic tutorial with example
React.js Basic Tutorial with Example
React is a popular JavaScript library for building user interfaces, particularly for single-page applications (SPAs) where you need a fast, interactive user experience. React allows you to build UI components that can update dynamically without needing to reload the whole page.
In this tutorial, we'll walk through the basic concepts of React and build a simple app from scratch. This example will include the following topics:
- Setting up a React project.
- Creating components.
- Using props and state.
- Handling events.
- Using hooks (specifically
useState).
Step 1: Setting Up a React Project
To start working with React, the easiest way is to use Create React App, a tool that sets up everything for you.
Open your terminal and run the following command to create a new React app:
This will create a new folder called
react-basic-tutorialwith all the necessary files and dependencies for your React app.After the setup is complete, navigate into the new folder:
Now, start the development server:
This will run the app and open it in your default web browser at
http://localhost:3000.
Step 2: Understanding the Folder Structure
The folder structure of a React app generated by Create React App looks like this:
- public/index.html: This is the main HTML file. React components are rendered into the
divwith theid="root". - src/index.js: This is the entry point for the React app. It renders the main
Appcomponent. - src/App.js: This is the default app component, where we can define our main UI and logic.
Step 3: Creating a Simple Component
A component in React is a function (or a class) that returns a piece of UI (JSX) that will be rendered to the screen. We can create a simple component to display a message.
Open
src/App.jsand modify it as follows:Now, when you save the file, React will re-render the app and display the message on the page. This is the simplest form of a component that returns some static content.
Step 4: Using Props
Props (short for "properties") allow you to pass data from a parent component to a child component. Let’s create a component that takes in a name prop and displays a greeting message.
First, create a new component called
Greeting.jsinside thesrcfolder:- This component receives
propsand renders a greeting message using thenameprop.
- This component receives
Now, go back to
App.jsand use theGreetingcomponent, passing anameprop:When you save the file, the app will display two greeting messages:
This demonstrates how you can reuse the Greeting component and pass different values for the name prop.
Step 5: Using State with useState Hook
State in React allows components to store data that can change over time. The useState hook is used to add state to functional components. Let's modify our app to include a counter with buttons to increase and decrease the count.
In
App.js, import theuseStatehook and create a state variable for the count:In this code:
useState(0)creates a state variablecountwith an initial value of0.setCountis the function that will update thecountvalue.incrementanddecrementare functions that modify thecountstate when the buttons are clicked.- The
onClickevent handlers call these functions when the user clicks the buttons.
When you save and run the app, you’ll see the following:
- A
Current countvalue. - Buttons to Increment and Decrement the count.
- A
Step 6: Handling Events
React allows you to handle user interactions, like clicks, form submissions, and keyboard inputs, with event handlers. We've already seen the onClick event in action with the increment and decrement buttons.
Let's add an input field that updates the greeting message dynamically when the user types their name.
Update
App.jsto include an input field that updates thenamestate:Here:
- We added a new state variable
namewith theuseStatehook to store the name entered by the user. - The
handleInputChangefunction updates thenamestate as the user types in the input field. - We use the
namestate as a prop for theGreetingcomponent to dynamically update the greeting message.
- We added a new state variable
When you run the app:
- You will see a text input where you can type a name.
- The greeting message will update in real-time as you type.
- The count can still be incremented and decremented.
Conclusion
In this tutorial, you learned the following basic concepts of React:
- Components: Building blocks of a React app.
- Props: Passing data from parent to child components.
- State: Managing data within components using the
useStatehook. - Event Handling: Responding to user interactions like button clicks and input changes.
By combining these concepts, you can start building more interactive and dynamic web applications with React. As you continue learning React, you can explore more advanced topics like component lifecycle, useEffect hook, context API, routing with React Router, and state management with Redux.
Comments
Post a Comment