InterviewSolution
| 1. |
Explain how to create a simple React Hooks example program. |
|
Answer» I will assume that you are having some coding knowledge about JavaScript and have installed Node on your system for creating a below given React HOOK program. An installation of Node comes along with the command-line tools: npm and npx, where npm is useful to install the packages into a project and npx is useful in running commands of Node from the command line. The npx looks in the current project folder for checking whether a command has been installed there. When the command is not available on your computer, the npx will look in the npmjs.com repository, then the latest version of the command script will be loaded and will run without locally installing it. This feature is useful in creating a skeleton React application within a few key presses. Open the Terminal inside the folder of your choice, and run the following command: npx create-react-app react-items-with-hooksHere, the create-react-app is an app initializer created by Facebook, to help with the easy and quick creation of React application, providing options to customize it while creating the application? The above command will create a new folder named react-items-with-hooks and it will be initialized with a basic React application. Now, you will be able to open the project in your favourite IDE. You can see an src folder inside the project along with the main application component App.js. This file is having a single function App() which will return an element and it will make use of an extended JavaScript syntax(JSX) for defining the component. JSX will permit you for writing HTML-style template syntax directly into the JavaScript file. This mixture of JavaScript and HTML will be converted by React toolchain into pure JavaScript that will render the HTML element. It is possible to define your own React components by writing a function that will return a JSX element. You can try this by creating a new file src/SearchItem.jsand put the following CODE into it. import React from 'react';export function SearchItem() { return ( <div> <div className="search-input"> <input type="TEXT" placeholder="SearchItem"/> </div> <h1 className="h1">Search Results</h1> <div className="items"> <table> <thead> <tr> <th className="itemname-col">Item Name</th> <th className="price-col">Price</th> <th className="quantity-col">Quantity</th> </tr> </thead> <tbody></tbody> </table> </div> </div> );}This is all about how you can create a component. It will only display the empty table and doesn’t do anything. But you will be able to use the Search component in the application. Open the file src/App.js and add the import statement given below to the top of the file. import { SearchItem } from './SearchItem';Now, from the logo.svg, import will be removed and then contents of returned VALUE in the function App() will be replaced with the following code: <div className="App"> <header> Items with Hooks </header> <SearchItem/></div>You can notice that the element <SearchItem/> has been used just similar to an HTML element. The JSX syntax will enable for including the components in this approach directly within the JavaScript code. Your application can be tested by running the below-given command in your terminal. npm start This command will compile your application and open your default browser into http://localhost:4000. This command can be kept on running when code development is in progress to make SURE that the application is up-to-date, and also this browser page will be reloaded each time you modify and save the code. This application will work finely, but it doesn’t look nice as it doesn’t react to any input from the user. You can make it more interactive by adding a state with React Hooks, adding authentication, etc. |
|