Define props parameter in functional component
In this step, you will create a new child component and prepare it to receive props. Functional components in React can accept a single argument, which is an object containing all the props passed to it. By convention, this object is named props.
First, let's navigate to our project directory. The setup script has already created a project named my-app.
cd ~/project/my-app
Next, we need to install the project dependencies.
npm install
Now, let's create a new component file. In the file explorer on the left, navigate to the src folder. Create a new file inside src and name it Greeting.jsx.
Add the following code to your newly created src/Greeting.jsx file. This defines a simple functional component that accepts a props parameter.
function Greeting(props) {
return <h2>Hello, World!</h2>;
}
export default Greeting;
Next, we need to import and use this new component in our main App component. Open the src/App.jsx file and modify it to look like this:
import Greeting from "./Greeting.jsx";
function App() {
return (
<>
<h1>Welcome to My App</h1>
<Greeting />
</>
);
}
export default App;
Now, let's start the development server to see our application.
npm run dev -- --host 0.0.0.0 --port 8080
After running the command, switch to the Web 8080 tab in the top-left corner of the interface. You should see your application running, displaying "Welcome to My App" and "Hello, World!".