Deliver the following REACT Application: Create a cross-platform application using React Native to do the following. Application should be able to run in one of the simulators/emulators (iOS/Android).
Allows to add a Product information (as an object) to an array using the following information:
Product ID (5-digit number)
Product Name
Product Category
Supplier
Price
• User can click on Display button to display all the array information on screen in a table format.
• User can continue adding products in the array and every time Display button is clicked, the table is updated accordingly.
• Your app should have 4 products already in an array (hard-coded).
• User can sort the table by price (high to low and low to high).
• User can delete a product from the table.
• User can edit the product data.
• User can also search for employees by Product ID (in this case, the table filters the products to show only the match (if no match is found, a message “No Product found” should be displayed).
• User can also search for products by Product Category (in this case, the table filters the products to show only the products with the same category). If no match is found, a message “No Products belong in this Category” should be displayed.
In this lab, we will be creating a cross-platform mobile application using React Native that allows users to manage a list of products. Users can add new products, view all products, sort the list by price, edit and delete products, and search for products by ID or category. We will be using the following technologies:
React Native
Expo
JavaScript
Prerequisites
Before starting this lab, you should have the following installed:
Node.js
Expo CLI
Expo Go App (for testing on mobile devices)
A code editor (such as VS Code)
Step 1: Set up the project
Open your terminal and create a new directory for the project.
Navigate to the new directory and run the following command to create a new React Native project:
expo init ProductManagementApp
Choose the "blank" template when prompted.
Once the project is created, navigate to the project directory and start the development server by running the following command:
npm start
This will open the Expo Developer Tools in your browser. From here, you can run the app in a simulator or emulator, or scan the QR code with the Expo Go app to test it on a mobile device.
Step 2: Create the ProductForm component
Create a new file called ProductForm.js in the components directory.
In this file, import the following:
importReact, { useState } from 'react';
import { View, Text, TextInput, Button } from 'react-native';
Create a functional component called ProductForm that takes in a prop called addProduct.
here's an example of what the code might look like in React:
In this code, ProductForm is a functional component that accepts a prop called addProduct. This prop is a function that takes in a product name and price and adds it to some kind of list or data structure.
The component contains two local state variables: productName and productPrice, which are initialized to empty strings and 0, respectively. These variables are updated whenever the user types into the two input fields using the onChange event handler.
When the user submits the form, the handleSubmit function is called. This function prevents the default form submission behavior (which would refresh the page) and instead calls the addProduct function passed in as a prop, passing in the current values of productName and productPrice. It then resets the two state variables to their initial values, clearing the input fields.
Finally, the component returns a form with two input fields for the product name and price, as well as a submit button that triggers the handleSubmit function when clicked.
Inside the component, define a state variable called product with the following properties:
const [product, setProduct] = useState({
productId: '',
productName: '',
productCategory: '',
supplier: '',
price: '',
});
Define a handleChange function that takes in an event object and updates the product state with the new input value:
consthandleChange = (event) => {
const { name, value } = event.target;
setProduct((prevProduct) => ({
...prevProduct,
[name]: value,
}));
};
Define a handleSubmit function that creates a new product object by copying the product state, then calls the addProduct prop with the new product object:
consthandleSubmit = () => {
const newProduct = { ...product };
addProduct(newProduct);
setProduct({
productId: '',
productName: '',
productCategory: '',
supplier: '',
price: '',
});
};
Render a form with input fields for each property of the product state, a submit button that calls the handleSubmit function, and a label for each input field.
In this code, ProductTable is a functional component that accepts a prop called products, as well as three methods for editing, deleting, and sorting the list.
Inside the component, we define a renderItem function that takes in a product object and returns a View component containing Text components for each property of the object (in this case, name and price). It also includes two TouchableOpacity components for editing and deleting the item, which call the editProduct and deleteProduct functions passed in as props, respectively.
In the ProductTablecomponent's return statement, we render two TouchableOpacity components for sorting the list by name or price. These components call the sortProducts function passed in as a prop with the corresponding sort criterion.
Finally, we use the map function to render each item in the products array by calling the renderItem function we defined earlier.