ReactJS – Example ”; Previous Next In this chapter, let us create a sample expense manager application by applying the concepts that we have learned in this tutorial. Some of the concepts are listed below − React basics (component, jsx, props and state) Router using react-router Http client programming (Web API) Form programming using Formik Advanced state management using Redux Async / await programming Features Some of the features of our sample expense manager application are − Listing all the expenses from the server Add an expense item Delete an expense item Here, Expense manager API Install necessary modules State management List expenses Add expense Print Page Previous Next Advertisements ”;
Category: reactjs
ReactJS – Portals
ReactJS – Portals ”; Previous Next Portals provides a way for a component to render its children into a DOM node outside its own DOM hierarchy. Portal can be used in model dialog, popups, tooltip, etc., where the parent (the rendering component) and child DOM node (model dialogs) are preferred to be rendered in different DOM node. Let us learn how portal works and how to apply it in our application in this chapter. Concept and usage of portals Let us consider that we have two DOM node in the main document as shown below − <div id=”root”></div> <div id=”modalRoot”></div> Here, root DOM node will be attached with main react component. modalRoot will be used by the react application whenever it needs to show a modal dialog by attaching the modal dialog into the modelRoot DOM node instead of rendering the model dialog inside its own DOM element. This will help to separate the modal dialog from the actual application. The separation of modal dialog from its parent DOM element will preserve it from the styling of its parent DOM element. The styling can be applied separately as modal dialogs, tooltips, etc., differs from its parent with regard to styling. React provides a special method createPortal in ReactDOM package to create a portal. The signature of the method is as follows − ReactDOM.createPortal(child, container) Here, child is the model dialogs, tooltips, etc., rendered by the parent component. render() { return ReactDOM.createPortal( this.props.children, // modal dialog / tooltips domNode // dom outside the component ); } container is the DOM element outside the parent DOM node (domNode in above example) Applying portals Let us create a new react application to learn how to apply portals in this section. First of all, create a new react application and start it using below command. create-react-app myapp cd myapp npm start Next, open App.css (src/App.css) and remove all CSS classes and include CSS for modal dialogs. .modal { position: absolute; top: 0; bottom: 0; left: 0; right: 0; display: grid; justify-content: center; align-items: center; background-color: rgba(0,0,0,0.2); } .modalContent { padding: 20px; background-color: #fff; border-radius: 2px; display: inline-block; min-height: 300px; margin: 1rem; position: relative; min-width: 300px; box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23); justify-self: center; } Next, open index.html (public/index.html) and add a DOM node to support portals <!DOCTYPE html> <html lang=”en”> <head> <link rel=”icon” href=”%PUBLIC_URL%/favicon.ico” /> <link rel=”apple-touch-icon” href=”%PUBLIC_URL%/logo192.png” /> <link rel=”manifest” href=”%PUBLIC_URL%/manifest.json” /> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div style=”padding: 10px;”> <div id=”root”></div> </div> <div id=”modalRoot”></div> </body> </html> Next, create a simple component, SimplePortal (src/Components/SimplePortal.js) and render a modal dialog as shown below − import React from “react”; import PortalReactDOM from ”react-dom” const modalRoot = document.getElementById(”modalRoot”) class SimplePortal extends React.Component { constructor(props) { super(props); } render() { return PortalReactDOM.createPortal( <div className=”modal” onClick={this.props.onClose} > <div className=”modalContent”> {this.props.children} <hr /> <button onClick={this.props.onClose}>Close</button> </div> </div>, modalRoot, ) } } export default SimplePortal; Here, createPortal to create a new portal and renders a modal dialog. Content of the modal dialog are retrieved from children of the component through this.props.children close button actions are handled through props and will be handled by the parent component. Next, open App component (src/App.js), and use SimplePortal component as shown below − import ”./App.css” import React from ”react”; import SimplePortal from ”./Components/SimplePortal” class App extends React.Component { constructor(props) { super(props); this.state = { modal: false } } handleOpenModal = () => this.setState({ modal: true }) handleCloseModal = () => this.setState({ modal: false }) render() { return ( <div className=”container”> <div style={{ padding: “10px” }}> <div> <div><p>Main App</p></div> <div> <button onClick={this.handleOpenModal}> Show Modal </button> { this.state.modal ? ( <SimplePortal onClose={this.handleCloseModal}> Hi, I am the modal dialog created using portal. </SimplePortal> ) : null} </div> </div> </div> </div> ); } } export default App; Here, Imported SimplePortal component Added a button to open the modal dialog Created a handler to open the modal dialog Created a handler to close the modal dialog and passed it to SimplePortal component through onClose props. Finally, open the application in the browser and check the final result. Summary React portal provides an easy way to access and handle DOM outside the component. It enables the event bubbling across the different DOM nodes without any extra effort. Print Page Previous Next Advertisements ”;
ReactJS – Quick Guide
Reactjs – Quick Guide ”; Previous Next ReactJS – Introduction ReactJS is a free and open-source front-end JavaScript library which is used to develop various interactive user-interfaces. It is a simple, feature rich and component based UI library. When we say component based, we mean that React develops applications by creating various reusable and independent codes. This UI library is, thus, widely used in web development. ReactJS can be used to develop small applications as well as big, complex applications. ReactJS provides minimal and solid feature set to kick-start a web application. React community compliments React library by providing large set of ready-made components to develop web application in a record time. React community also provides advanced concept like state management, routing, etc., on top of the React library. React versions Reactjs library was founded by Jordan Walke, a software engineer at Facebook in 2011. Then the initial version, 0.3.0 of React is released on May, 2013 and the latest version, 17.0.1 is released on October, 2020. The major version introduces breaking changes and the minor version introduces new feature without breaking the existing functionality. Bug fixes are released as and when necessary. React follows the Semantic Versioning (semver) principle. What is the need for ReactJS? Even though there are various libraries that provide a medium to develop user-interfaces, ReactJS still stands tall in terms of popularity. Here”s why − Component Based − ReactJS makes use of multiple components to build an application. These components are independent and have their own logic which makes them reusable throughout the development process. This will drastically reduce the application”s development time. Better and Faster Performance − ReactJS uses Virtual DOM. Virtual DOM compares the previous states of components of an application with the current states and only updates the changes in Real DOM. Whereas, conventional web applications update all components again. This helps ReactJS in creating web applications faster. Extremely Flexible − React allows developers and teams to set their own conventions that they deem are best suited and implement it however they see fit, as there are no strict rules for code conventions in React. Creates dynamic applications easily − Dynamic web applications require less coding while offering more functionality. Thus, ReactJS can create them easily. Develops Mobile Applications as well − Not only web applications, React can also develop mobile applications using React Native. React Native is an open-source UI software framework that is derived from React itself. It uses React Framework to develop applications for Android, macOS, Web, Windows etc. Debugging is Easy − The data flow in React is unidirectional, i.e., while designing an app using React, child components are nested within parent components. As the data flows is in a single direction, it gets easier to debug errors and spot the bugs. Applications Few popular websites powered by React library are listed below − Facebook, popular social media application − React was originally developed at Facebook (or Meta), so its only natural that they use it to run their application. As for their mobile application, it uses React Native to display Android and iOS components, instead of DOM. Facebook”s codebase now includes over 20,000 components and uses the React version that is public. Instagram, popular photo sharing application − Instagram is also completely based on React, as it is powered by Meta as well. The main features to show its usage include Geo-locations, Hashtags, Google Maps APIs etc. Netflix, popular media streaming application − Netflix switched to React in 2015. The factors that mainly influenced this decision are the 1) startup speed to reduce the processing time to render the homepage and enabling dynamic elements in the UI, 2) modularity to allow various features that must coexist with the control experience and 3) runtime performance for efficient UI rendering. Code Academy, popular online training application − Code Academy uses React as the “script is battle-tested, easy to think about, makes SEO easy and is compatible with legacy code and flexible enough for the future”. Reddit, popular content sharing application − Reddit is also developed using React from the scratch. As you see, most popular application in every field is being developed by React Library. ReactJS – Installation This chapter explains the installation of React library and its related tools in your machine. Before moving to the installation, let us verify the prerequisite first. React provides CLI tools for the developer to fast forward the creation, development and deployment of the React based web application. React CLI tools depends on the Node.js and must be installed in your system. Hopefully, you have installed Node.js on your machine. We can check it using the below command − node –version You could see the version of Nodejs you might have installed. It is shown as below for me, v14.2.0 If Nodejs is not installed, you can download and install by visiting https://nodejs.org/en/download/. Toolchain To develop lightweight features such as form validation, model dialog, etc., React library can be directly included into the web application through content delivery network (CDN). It is similar to using jQuery library in a web application. For moderate to big application, it is advised to write the application as multiple files and then use bundler such as webpack, parcel, rollup, etc., to compile and bundle the application before deploying the code. React toolchain helps to create, build, run and deploy the React application. React toolchain basically provides a starter project template with all necessary code to bootstrap the application. Some of the popular toolchain to develop React applications are − Create React App − SPA oriented toolchain Next.js − server-side rendering oriented toolchain Gatsby − Static content oriented toolchain Tools required to develop a React application are
ReactJS – Accessibility
ReactJS – Accessibility ”; Previous Next Accessibility (a11y) is designing the web application in such a way that the application will be accessible by everyone and support assistive technology to read the content of the application for the end user. React supports all the aspects of accessibility in a web application. Let us see how react supports accessibility in this chapter. ARIA (aria-*) attributes WAI-ARIA (Web Accessibility Initiative – Accessible Rich Internet Applications) is a standard specifying ways to build fully accessible JavaScript widgets. It provides a large set of HTML attributes (aria-*) to support accessibility. React supports all those attributes in its components. In general, React restricts the HTML attributes to be in the form of camelCase, but for accessibility attributes, it should be in the form of kebab-case or lisp-case or simply as it is in the HTML document. For example, the below code shows how to use the HTML accessibility attributes. <input type=”text” aria-label={labelText} aria-required=”true” name=”name” /> Here, aria-label is used to specify the label of the input element aria-required is used to specify that the input should be filled. Note that the attributes are use as it is (in kebab-case format). Sematic HTML A web document coded by applying the sematic HTML (article, section, navigation, etc.,) tags improves the accessibility of the document. In react, there are situation where we use blocks (div) just to satisfy the react framework. For example, react does not support multiple tags in its render code. To overcome the restriction, developer may use parent tag (div) to make the multiple tags as children. function ShowItems({ data }) { return ( <div> <dt>{data.title}</dt> <dd>{data.description}</dd> </div> ); } React provides Fragment component to work around the scenario. We can just replace Fragment instead of div as shown below − function ShowItems({ data }) { return ( <Fragment> <dt>{data.title}</dt> <dd>{data.description}</dd> </Fragment> ); } Forms Every input should be labeled and the label should be descriptive to understand the input element. React provides a special props htmlFor to specify the input element for the specific description. Developer can use it create accessible forms. <label htmlFor=”firstName”>Firstname:</label> <input id=”firstName” type=”text” name=”name”/> Keyboard support Keyboard support is a must for creating accessible web application. Some of the features expecting keyboard support are, Focus − React provides a concept called Ref to access the raw DOM element. When the application needs raw access to DOM element, Ref and Forwarding Ref can be used to manage the raw DOM element. Skip links − Skip navigation links are must feature to support accessibility. They allows the user to skip all the navigation in one go when accessing the application using only keyboard. It can be done using smart anchor tags, which are fully supported by react. <body> <a href=”#maincontent”>Skip to main content</a> … <main id=”maincontent”> … </main> Mouse and pointer functionality − To create a true accessible application, all the feature should be accessible through keyboard. Component with high level mouse and pointer based user interaction should be changed to accommodate keyboard only user interaction. React provides all event handling logic to modify the default mouse based UI to keyboard based UI. Aria components React community provides many components with full accessibility support. They can be used as it is without any modification. They automatically enables the application to be accessible. Some of the third party components with aria support are as follows − react-aria − react-aria provides large set of react component with full accessibility support react-modal − react-modal provides modal component with aria support. react-aria-modal − react-aria-modal is yet another modal component with aria support. react-select − react-select provides select component with aria support. react-dropdown-aria − react-dropdown-aria provides dropdown component with aria support. react-aria-menubutton − react-aria-menubutton provides menu button component with aria support. react-aria-tabpanel − react-aria-tabpanel provides tab panel component with aria support. Summary React provides many features to create fully accessible, aria supported web application. Creation of accessible application is always a challenge and react reduces the burden a bit in the form of ready-made component as well as core feature to write a accessible application from the scratch. Print Page Previous Next Advertisements ”;
ReactJS – DOM
ReactJS – DOM ”; Previous Next To run a react application, it needs to be attached itself to the main document of the web application. React provides a module to access and attach the application to the DOM of the document and the module is ReactDOM (react-dom). Let us learn how to create a simple react component and attach the component into the document using reactDOM module in this chapter. ReactDOM usage react-dom is the core package used to manipulate the DOM of the document. react-dom allows one or more react application to be attached to the document. The react-dom should be imported into the application as shown below − import * as ReactDOM from ”react-dom”; The react-dom provides two method to manipulate the DOM and they are as follows − createPortal() − Create a portal in the react application. A portal is a special react node, which enables the main react application to render it”s children into the DOM outside of it”s own hierarchy of the DOM component. return ReactDOM.createPortal( this.props.children, // child node domNode // DOM node outside the root element ); Let us learn the portals in more details in the upcoming chapters. flushSync() − Flushes the state changes immediately and updates the DOM. Generally, react creates a virtual dom and then updates the real DOM by analyzing the differences between virtual and real DOM. The update frequency is determined internally by react. flushSync() interrupts and update the changes immediately. The react-dom provides two submodules, one for server side application and another for client side application. The modules are as follows − react-dom/server react-dom/client ReactDOMServer Server module will be used to render a react component in the server and the module can be imported as shown below − import * as ReactDOMServer from ”react-dom/server”; Some of the methods provided by ReactDOMServer are as follows − renderToPipeableStream() − Render a react component to its initial HTML and returns a pipe stream. renderToReadableStream() − Render a react component to its initial HTML and returns a readable web stream through promise. renderToStaticNodeStream() − Render a react component to its initial HTML and returns a readable nodejs stream that outputs a HTML string. It skips extra markup such as data-reactroot and the output will be same as renderToStaticMarkup(). renderToString() − Render a react component to its initial HTML and returns a HTML string. renderToStaticMarkup() −Same as renderToString() except it skips extra markup such as data-reactroot. ReactDOMClient Client module will be extensively used in the front-end development and can be imported into the application as shown below − import * as ReactDOM from ”react-dom/client”; Some of the methods provided by ReactDOMClient are as follows − createRoot() − Create a root element to attach and render a react component later. It accepts a html element and returns a react node. The react node is called as root of the application. The returned react node will have two method, render to render a react component and unmount to unmount the react component. const root = createRoot(container); root.render(element); // where element = document.getElementById(”root-id”) root.umount(); hydrateRoot() − Same as createRoot() but it is used in combination with react-dom/server module to hydrate the react component rendered in the server. Applying ReactDOMClient First of all, create a new react application and start it using below command. create-react-app myapp cd myapp npm start Next, create a react component, Hello under component folder (src/components/Hello.js). import React from “react”; class Hello extends React.Component { constructor(props) { super(props) } render() { return ( <div>Hello, {this.props.name}</div> ); } } export default Hello; Next, open index.html (public/index.html) and add a new container (root2) as shown below − <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”utf-8″ /> <link rel=”icon” href=”%PUBLIC_URL%/favicon.ico” /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <meta name=”theme-color” content=”#000000″ /> <meta name=”description” content=”Web site created using create-react-app” /> <link rel=”apple-touch-icon” href=”%PUBLIC_URL%/logo192.png” /> <link rel=”manifest” href=”%PUBLIC_URL%/manifest.json” /> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div style=”padding: 10px;”> <div id=”root”></div> <div id=”root2″></div> </div> </body> </html> Next, open index.js (src/index.js) and attach our hello component into the root and root2 container as shown below − import React from ”react”; import ReactDOM from ”react-dom/client”; import ”./index.css”; import Hello from ”./Components/Hello”; import reportWebVitals from ”./reportWebVitals”; const root = ReactDOM.createRoot(document.getElementById(”root”)); root.render( <React.StrictMode> <Hello name=”Main root container” /> </React.StrictMode> ); const root2 = ReactDOM.createRoot(document.getElementById(”root2”)); root2.render( <React.StrictMode> <Hello name=”Another root container” /> </React.StrictMode> ); reportWebVitals();` Finally, open the application in the browser and check the result. The react component will be attached to both the root element as shown below − Summary ReactDOM provides the ability to create the entry point for the react application by attaching the react component into the HTML document in both client and server environment. Print Page Previous Next Advertisements ”;
ReactJS – Formik
ReactJS – Formik ”; Previous Next Formik is third party React form library. It provides basic form programming and validation. It is based on controlled component and greatly reduces the time to do form programming. The nature of form programming needs the state to be maintained. Because, the input field information will get changed as the user interacts with the form. But as we learned earlier, React library does not store or maintain any state information by itself and component has to use state management API to manage state. Considering this, React provides two types of components to support form programming. Expense Form Using Formik In this chapter, let us recreate the expense form using Formik library. Step 1 − First, create a new react application, react-formik-app using Create React App or Rollup bundler by following instruction in Creating a React application chapter. Step 2 − Install the Formik library. cd /go/to/workspace npm install formik –save Step 3 − Open the application in your favorite editor. Create src folder under the root directory of the application. Create components folder under src folder. Step 4 − Create a file, ExpenseForm.css under src folder to style the component. input[type=text], input[type=number], input[type=date], select { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } input[type=submit] { width: 100%; background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover { background-color: #45a049; } input:focus { border: 1px solid #d9d5e0; } #expenseForm div { border-radius: 5px; background-color: #f2f2f2; padding: 20px; } #expenseForm span { color: red; } Step 5 − Create another file, ExpenseForm.js under src/components folder and start editing. Import React and Formik library. import React from ”react”; import { Formik } from ”formik”; Next, import ExpenseForm.css file. import ”./ExpenseForm.css” Next, create ExpenseForm class. class ExpenseForm extends React.Component { constructor(props) { super(props); } } Set initial values of the expense item in the constructor. this.initialValues = { name: ””, amount: ””, date: ””, category: ”” } Next, create a validation method. Formik will send the current values entered by the user. validate = (values) => { const errors = {}; if (!values.name) { errors.name = ”Required”; } if (!values.amount) { errors.amount = ”Required”; } if (!values.date) { errors.date = ”Required”; } if (!values.category) { errors.category = ”Required”; } return errors; } Create a method to submit the form. Formik will send the current values entered by the user. handleSubmit = (values, setSubmitting) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); } Create render() method. Use handleChange, handleBlur and handleSubmit method provided by Formik as input elements event handler. render() { return ( <div id=”expenseForm”> <Formik initialValues={this.initialValues} validate={values => this.validate(values)} onSubmit={(values, { setSubmitting }) => this.handleSubmit(values, setSubmitting)} >{ ({ values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, /* and other goodies */ }) => ( <form onSubmit={handleSubmit}> <label for=”name”>Title <span>{errors.name && touched.name && errors.name}</span></label> <input type=”text” id=”name” name=”name” placeholder=”Enter expense title” onChange={handleChange} onBlur={handleBlur} value={values.name} /> <label for=”amount”>Amount <span>{errors.amount && touched.amount && errors.amount}</span></label> <input type=”number” id=”amount” name=”amount” placeholder=”Enter expense amount” onChange={handleChange} onBlur={handleBlur} value={values.amount} /> <label for=”date”>Spend Date <span>{errors.date && touched.date && errors.date}</span></label> <input type=”date” id=”date” name=”date” placeholder=”Enter date” onChange={handleChange} onBlur={handleBlur} value={values.date} /> <label for=”category”>Category <span>{errors.category && touched.category && errors.category}</span></label> <select id=”category” name=”category” onChange={handleChange} onBlur={handleBlur} value={values.category}> <option value=””>Select</option> <option value=”Food”>Food</option> <option value=”Entertainment”>Entertainment</option> <option value=”Academic”>Academic</option> </select> <input type=”submit” value=”Submit” disabled={isSubmitting} /> </form> ) } </Formik> </div> ) } Finally, export the component. export default ExpenseForm The complete code of the ExpenseForm component is given below. import React from ”react”; import ”./ExpenseForm.css” import { Formik } from ”formik”; class ExpenseFormik extends React.Component { constructor(props) { super(props); this.initialValues = { name: ””, amount: ””, date: ””, category: ”” } } validate = (values) => { const errors = {}; if (!values.name) { errors.name = ”Required”; } if (!values.amount) { errors.amount = ”Required”; } if (!values.date) { errors.date = ”Required”; } if (!values.category) { errors.category = ”Required”; } return errors; } handleSubmit = (values, setSubmitting) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); } render() { return ( <div id=”expenseForm”> <Formik initialValues={this.initialValues} validate={values => this.validate(values)} onSubmit={(values, { setSubmitting }) => this.handleSubmit(values, setSubmitting)} > { ({ values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, /* and other goodies */ }) => ( <form onSubmit={handleSubmit}> <label for=”name”>Title <span>{errors.name && touched.name && errors.name}</span></label> <input type=”text” id=”name” name=”name” placeholder=”Enter expense title” onChange={handleChange} onBlur={handleBlur} value={values.name} /> <label for=”amount”>Amount <span>{errors.amount && touched.amount && errors.amount}</span></label> <input type=”number” id=”amount” name=”amount” placeholder=”Enter expense amount” onChange={handleChange} onBlur={handleBlur} value={values.amount} /> <label for=”date”>Spend Date <span>{errors.date && touched.date && errors.date}</span></label> <input type=”date” id=”date” name=”date” placeholder=”Enter date” onChange={handleChange} onBlur={handleBlur} value={values.date} /> <label for=”category”>Category <span>{errors.category && touched.category && errors.category}</span></label> <select id=”category” name=”category” onChange={handleChange} onBlur={handleBlur} value={values.category}> <option value=””>Select</option> <option value=”Food”>Food</option> <option value=”Entertainment”>Entertainment</option> <option value=”Academic”>Academic</option> </select> <input type=”submit” value=”Submit” disabled={isSubmitting} /> </form> ) } </Formik> </div> ) } } export default ExpenseForm; index.js Create a file, index.js under the src folder and use ExpenseForm component. import React from ”react”; import ReactDOM from ”react-dom”; import ExpenseForm from ”./components/ExpenseForm” ReactDOM.render( <React.StrictMode> <ExpenseForm /> </React.StrictMode>, document.getElementById(”root”) ); index.html Finally, create a public folder under the root folder and create index.html file. <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”utf-8″> <title>React App</title> </head> <body> <div id=”root”></div> <script type=”text/JavaScript” src=”./index.js”></script> </body> </html> Serve the application using npm command. npm start Open the browser and enter http://localhost:3000 in the address bar and press enter. Finally, enter a sample expense detail and click submit. The submitted data will be collected and showed in a popup message box. The interactive version of the
ReactJS – Keys
ReactJS – Keys ”; Previous Next List and keys We learned how to use collections in React using for loop and map function in previous chapters. If we run the application, it output as expected. If we open the developer console in the browser, then it will show a warning as shown below − Warning: Each child in a list should have a unique “key” prop. Check the render method of `ExpenseListUsingForLoop`. See https://reactjs.org/link/warning-keys for more information. tr ExpenseListUsingForLoop@ div App So, what does it means and how it affects our React application. As we know, React tries to render only the updated values in DOM through various mechanism. When the React renders a collection, it tries to optimize the rendering by updating only the updated item in the list. But, there is no hint for the React to find which items are new, updated or removed. To get the information, React allows a key attributes for all components. The only requirement is that the value of the key should be unique among the current collection. Let us recreate one of our earlier application and apply the key attributes. create-react-app myapp cd myapp npm start Next, create a component, ExpenseListUsingForLoop under components folder (src/components/ExpenseListUsingForLoop.js). import React from ”react” class ExpenseListUsingForLoop extends React.Component { render() { return <table> <thead> <tr> <th>Item</th> <th>Amount</th> </tr> </thead> <tbody> </tbody> <tfoot> <tr> <th>Sum</th> <th></th> </tr> </tfoot> </table> } } export default ExpenseListUsingForLoop Here, we created a basic table structure with header and footer. Then, create a function to find the total expense amount. We will use it later in the render method. getTotalExpenses() { var items = this.props[”expenses”]; var total = 0; for(let i = 0; i < items.length; i++) { total += parseInt(items[i]); } return total; } Here, getTotalExpenses loop over the expense props and summarize the total expenses. Then, add expense items and total amount in the render method. render() { var items = this.props[”expenses”]; var expenses = [] expenses = items.map((item, idx) => <tr key={idx}><td>item {idx + 1}</td><td>{item}</td></tr>) var total = this.getTotalExpenses(); return <table> <thead> <tr> <th>Item</th> <th>Amount</th> </tr> </thead> <tbody> {expenses} </tbody> <tfoot> <tr> <th>Sum</th> <th>{total}</th> </tr> </tfoot> </table> } Here, Navigated each item in the expense array using map function, created table row (tr) for each entry using transform function and finally set the returned array in expenses variable. Set the key attributes for each row with the index value of the item. Used expenses array in the JSX expression to include the generated rows. Used getTotalExpenses method to find the total expense amount and add it into the render method. The complete source code of the ExpenseListUsingForLoop component is as follows − import React from ”react” class ExpenseListUsingForLoop extends React.Component { getTotalExpenses() { var items = this.props[”expenses”]; var total = 0; for(let i = 0; i < items.length; i++) { total += parseInt(items[i]); } return total; } render() { var items = this.props[”expenses”]; var expenses = [] expenses = items.map( (item, idx) => <tr key={idx}><td>item {idx + 1}</td><td>{item}</td></tr>) var total = this.getTotalExpenses(); return <table> <thead> <tr> <th>Item</th> <th>Amount</th> </tr> </thead> <tbody> {expenses} </tbody> <tfoot> <tr> <th>Sum</th> <th>{total}</th> </tr> </tfoot> </table> } } export default ExpenseListUsingForLoop Next, update the App component (App.js) with ExpenseListUsingForLoop component. import ExpenseListUsingForLoop from ”./components/ExpenseListUsingForLoop”; import ”./App.css”; function App() { var expenses = [100, 200, 300] return ( <div> <ExpenseListUsingForLoop expenses={expenses} /> </div> ); } export default App; Next, add include a basic styles in App.css. /* Center tables for demo */ table { margin: 0 auto; } div { padding: 5px; } /* Default Table Style */ table { color: #333; background: white; border: 1px solid grey; font-size: 12pt; border-collapse: collapse; } table thead th, table tfoot th { color: #777; background: rgba(0,0,0,.1); text-align: left; } table caption { padding:.5em; } table th, table td { padding: .5em; border: 1px solid lightgrey; } Next, check the application in the browser. It will show the expenses as shown below − Finally, open the developer console and find that the warning about key is not shown. Key and index We learned that the key should be unique to optimize the rendering of the component. We used index value and the error is gone. Is it still correct way to provide value for the list? The answer is Yes and No. Setting the index key will work in most situation but it will behave in unexpected ways if we use uncontrolled component in our application. Let us update our application and add two new features as specified below − Add an input element to each row adjacent to expense amount. Add a button to remove the first element in the list. First of all, add a constructor and set the initial state of the application. As we are going to remove certain item during runtime of our application, we should use state instead of props. constructor(props) { super(props) this.state = { expenses: this.props[”expenses”] } } Next, add a function to remove the first element of the list. remove() { var itemToRemove = this.state[”expenses”][0] this.setState((previousState) => ({ expenses: previousState[”expenses”].filter((item) => item != itemToRemove) })) } Next, bind the remove function in the constructor as shown below − constructor(props) { super(props) this.state = { expenses: this.props[”expenses”] } this.remove = this.remove.bind(this) } Next, add a button below the table and set remove function in its onClick action. render() { var items = this.state[”expenses”]; var expenses = [] expenses = items.map( (item, idx) => <tr key={idx}><td>item {idx + 1}</td><td>{item}</td></tr>) var total = this.getTotalExpenses(); return ( <div> <table> <thead> <tr> <th>Item</th> <th>Amount</th> </tr> </thead> <tbody> {expenses} </tbody> <tfoot> <tr> <th>Sum</th> <th>{total}</th> </tr> </tfoot> </table> <div> <button onClick={this.remove}>Remove first item</button> </div> </div>
ReactJS – Strict Mode
ReactJS – Strict Mode ”; Previous Next React is build on the concept of component, properties and state. React provides a relatively few set of API to create and update components. The basic features of React is enough for large part of the our front end application. But, still modern application has complex scenarios, which needs advanced front-end logic. React provides lot of high-level API, which will help us to create complex front-end application. The high-level API comes with a cost. High-level API are little bit tough to learn and apply it in our application. React provides a strict mode, which helps the developer to properly apply the high-level API by highlighting the potential problem during development phase of our application. As we know, everything in the React is build on the concept of components, strict mode is simply a non-visual component, React.StrictMode. The usage of strict mode component is also very simple. Just wrap the component to be analyzed with React.StrictMode component as shown below − <React.StrictMode> <OurComplexComponent /> </React.StrictMode> Another functionality of the strict mode is to throw error or warning when certain legacy or depreciated API (which will eventually introduce bugs in the application) are used in the application. Let us learn the list of items highlighted by strict mode component in this chapter. Unsafe lifecycle usage React identifies some of the legacy lifecycle events are unsafe to use in the async based application. They are as follows − componentWillMount componentWillReceiveProps componentWillUpdate Unsafe means it will create hard to debug bugs in the application. React will eventually remove these unsafe lifecycle event in the future version of the library. Until then, developer are alerted when they use legacy unsafe lifecycle events. Legacy ref API usage Earlier version of React uses string based ref management and later added callback based ref management. The recommended way is to use callback based option due to its stability and less error-prone nature. Strict mode will throw warning if we use string based option. The most recent version of React provides improved ref management option, which is both easy to code and less error-prone nature. Legacy findDOMNode usage findDOMNode helps to search the tree of DOM node given a class instance. The usage of findDOMNode is depreciated and promotes the usage of ref based DOM management. Side effects React has two major phases, render and commit. render phase involves major work and time-consuming and commit phase is straight forward and fast. React introduces a concurrent mode, which will improve the performance of the render phase. One of the requirement of the concurrent mode is that the lifecycle event used in the render phase should not contain side effects. React will try to find the unexpected side effects using different approaches during development phase and report it as warning. Legacy context API Legacy context API is error-prone and it is not recommended to use. It will be removed in the future version. Until then, strict mode will detect the usage of legacy context API and report it. Reusable state Components with reusable state can be mounted and destroyed multiple time without any side effects and will help to improve the performance of the application. The future version of the React library will add or remove section of UI while preserving state. React 18 introduces a strict mode feature, which will try to unmount and remount the component to ensure the component is resilient. Any issue during this phase will be reported. Print Page Previous Next Advertisements ”;
ReactJS – Inline Style
ReactJS – Inline Style ”; Previous Next React provides a unique way of directly writing the CSS in the react component and using it inside JSX. The concept is called CSS in JS and provides many advantages over traditional usage of styles. Let us learn what is inline styling and how to use it react component. Concept of inline styling CSS enables the developer to design the UI of the web application. React provides first class support for CSS and allows CSS to be imported directly into the react application. Directly importing CSS into a react component is as simple as importing a package. import ”./App.css” But, importing css directly into the web component has one major disadvantage, Global namespace. Global style may affect the style of an individual component if there is a conflict in the class name. Developer need to be careful to assign some prefix to make sure that the conflict will not happen. The other way around is to allow the Javascript to manage the CSS and it is called CSS in JS. React allows the CSS to be used inside the JSX through special CSS syntax. React provides a style props for every component, which can be used specify inline style. The inline style should be provided in a Javascript object. The object should follows the below mentioned rules, The object key should be CamelCased version of normal CSS attributes. For example, the background-color should be specified as backgroundColor. { backgroundColor: “red” } The object value should be one of the allowed values of the corresponding object key in CSS and should be in string format. For example, the font-size CSS attributes and its value (12px) should be specified as follows − { fontSize: “12px” } React will handle the conflict and render the application correctly. Applying inline style Let us learn how to apply inline style in a react application in this section. First of all, create a new react application and start it using below command. create-react-app myapp cd myapp npm start Next, open App.css (src/App.css) and remove all the CSS classes. // remove the css Next, create a simple component, SimpleStyle (src/Components/SimpleIcon.js) as shown below − import React from “react”; class SimpleStyle extends React.Component { displayStyle = { fontFamily: ”Times New Roman”, fontSize: “24px”, color: “red” } render() { return ( <div> <div style={this.displayStyle}> Sample text to understand inline style (object as variable) in React component </div> <hr /> <div style={{ fontFamily: ”Arial”, fontSize: “24px”, color: “grey”}}> Sample text to understand inline style (object as expression) in React component </div> </div> ) } } export default SimpleStyle Here we have, Styled the first div using variable (displayStyle). Styled the second div using expression. Next, open App component (src/App.js) and update the content with SimpleStyle component as shown below − import ”./App.css” import React from ”react”; import SimpleStyle from ”./Components/SimpleStyle” function App() { return ( <div className=”container”> <div style={{ padding: “10px” }}> <div> <SimpleStyle /> </div> </div> </div> ); } export default App; Here we have, Imported the SimpleStyle component. Used SimpleStyle component to render the calender icon. Finally, open the application in the browser. The content will be rendered as shown below − Summary Inline styling helps developer to quickly include the CSS style without worrying about the conflict of the CSS styles. Also, the syntax is very similar to CSS and it makes it easy for the developer to use the feature without much learning curve. Print Page Previous Next Advertisements ”;
ReactJS – Pagination
ReactJS – Pagination ”; Previous Next React provides pagination component through third party UI component library. React community provides a large collection of UI / UX components and it is tough to choose the right library for our requirement. Bootstrap UI library is one of the popular choice for the developer and it is extensively used. React Bootstrap (https://react-bootstrap.github.io/) has ported almost all the bootstrap UI components and it has best support for Pagination component as well. Let us learn how to use Pagination component from react-bootstrap library in this chapter. Pagination component Pagination component allows the developer to create simple pagination with bootstrap design in a web application. Pagination component accepts below components. Pagination.Item Pagination.First Pagination.Last Pagination.Previous Pagination.Next Pagination component accepts a small set of props to customize the pagination component and they are as follows − size (sm | lg) Set the size of the pagination buttons bsPrefix (string) Enables to change the underlying component CSS Pagination.Item component accepts a small set of props to customize the pagination component and they are as follows − active (boolean) Set the item as active one and don”t render a tag. activeLabel (string) Label to indicate the state of the pagination item disabled (boolean) Disable the pagination item href (string) Link for the pagination item onClick (function) Callback function to be called when onClick event is fired Applying Pagination component First of all, create a new react application and start it using below command. create-react-app myapp cd myapp npm start Next, install the bootstrap library using below command, npm install –save bootstrap react-bootstrap Next, open App.css (src/App.css) and remove all the CSS classes. // remove the css Next, create a simple pagination component, SimplePagination (src/Components/SimplePagination.js) as shown below − import React from ”react”; import { Table, Pagination } from ”react-bootstrap”; class SimplePagination extends React.Component { render() { <div>Pagination component</div> } } export default SimplePagination; Next, create a file, users.json under public folder and populate with below user information, [ { “id”:1, “name”:”Fowler”, “age”:18 }, { “id”:2, “name”:”Donnell”, “age”:24 }, { “id”:3, “name”:”Pall”, “age”:26 }, { “id”:4, “name”:”Christos”, “age”:19 }, { “id”:5, “name”:”Dud”, “age”:29 }, { “id”:6, “name”:”Rayner”, “age”:22 }, { “id”:7, “name”:”Somerset”, “age”:31 }, { “id”:8, “name”:”Stavros”, “age”:32 }, { “id”:9, “name”:”Cody”, “age”:19 }, { “id”:10, “name”:”Sharai”, “age”:19 }, { “id”:11, “name”:”Kristo”, “age”:28 }, { “id”:12, “name”:”Harvey”, “age”:27 }, { “id”:13, “name”:”Christen”, “age”:27 }, { “id”:14, “name”:”Hillard”, “age”:19 }, { “id”:15, “name”:”Jaine”, “age”:32 }, { “id”:16, “name”:”Annabel”, “age”:29 }, { “id”:17, “name”:”Hildagarde”, “age”:29 }, { “id”:18, “name”:”Cherlyn”, “age”:18 }, { “id”:19, “name”:”Herold”, “age”:32 }, { “id”:20, “name”:”Gabriella”, “age”:32 }, { “id”:21, “name”:”Jessalyn”, “age”:32 }, { “id”:22, “name”:”Opal”, “age”:31 }, { “id”:23, “name”:”Westbrooke”, “age”:27 }, { “id”:24, “name”:”Morey”, “age”:22 }, { “id”:25, “name”:”Carleton”, “age”:26 }, { “id”:26, “name”:”Cosimo”, “age”:22 }, { “id”:27, “name”:”Petronia”, “age”:23 }, { “id”:28, “name”:”Justino”, “age”:32 }, { “id”:29, “name”:”Verla”, “age”:20 }, { “id”:30, “name”:”Lanita”, “age”:18 }, { “id”:31, “name”:”Karlik”, “age”:23 }, { “id”:32, “name”:”Emmett”, “age”:22 }, { “id”:33, “name”:”Abran”, “age”:26 }, { “id”:34, “name”:”Holly”, “age”:23 }, { “id”:35, “name”:”Beverie”, “age”:23 }, { “id”:36, “name”:”Ingelbert”, “age”:27 }, { “id”:37, “name”:”Kailey”, “age”:30 }, { “id”:38, “name”:”Ralina”, “age”:26 }, { “id”:39, “name”:”Stella”, “age”:29 }, { “id”:40, “name”:”Ronnica”, “age”:20 }, { “id”:41, “name”:”Brucie”, “age”:20 }, { “id”:42, “name”:”Ryan”, “age”:22 }, { “id”:43, “name”:”Fredek”, “age”:20 }, { “id”:44, “name”:”Corliss”, “age”:28 }, { “id”:45, “name”:”Kary”, “age”:32 }, { “id”:46, “name”:”Kaylee”, “age”:21 }, { “id”:47, “name”:”Haskell”, “age”:25 }, { “id”:48, “name”:”Jere”, “age”:29 }, { “id”:49, “name”:”Kathryne”, “age”:31 }, { “id”:50, “name”:”Linnea”, “age”:21 }, { “id”:51, “name”:”Theresina”, “age”:24 }, { “id”:52, “name”:”Arabela”, “age”:32 }, { “id”:53, “name”:”Howie”, “age”:22 }, { “id”:54, “name”:”Merci”, “age”:21 }, { “id”:55, “name”:”Mitchel”, “age”:30 }, { “id”:56, “name”:”Clari”, “age”:18 }, { “id”:57, “name”:”Laurena”, “age”:19 }, { “id”:58, “name”:”Odessa”, “age”:30 }, { “id”:59, “name”:”Pippy”, “age”:25 }, { “id”:60, “name”:”Wilmar”, “age”:23 }, { “id”:61, “name”:”Cherianne”, “age”:24 }, { “id”:62, “name”:”Huberto”, “age”:25 }, { “id”:63, “name”:”Ariella”, “age”:26 }, { “id”:64, “name”:”Lorant”, “age”:30 }, { “id”:65, “name”:”Francesca”, “age”:25 }, { “id”:66, “name”:”Ingamar”, “age”:28 }, { “id”:67, “name”:”Myrta”, “age”:27 }, { “id”:68, “name”:”Nicolette”, “age”:26 }, { “id”:69, “name”:”Petra”, “age”:22 }, { “id”:70, “name”:”Cyrill”, “age”:27 }, { “id”:71, “name”:”Ad”, “age”:23 }, { “id”:72, “name”:”Denys”, “age”:22 }, { “id”:73, “name”:”Karilynn”, “age”:23 }, { “id”:74, “name”:”Gunner”, “age”:30 }, { “id”:75, “name”:”Falkner”, “age”:20 }, { “id”:76, “name”:”Thurston”, “age”:19 }, { “id”:77, “name”:”Codi”, “age”:30 }, { “id”:78, “name”:”Jacob”, “age”:31 }, { “id”:79, “name”:”Gasparo”, “age”:26 }, { “id”:80, “name”:”Mitzi”, “age”:29 }, { “id”:81, “name”:”Rubetta”, “age”:21 }, { “id”:82, “name”:”Clary”, “age”:20 }, { “id”:83, “name”:”Oliviero”, “age”:24 }, { “id”:84, “name”:”Ranique”, “age”:21 }, { “id”:85, “name”:”Shae”, “age”:24 }, { “id”:86, “name”:”Woodrow”, “age”:20 }, { “id”:87, “name”:”Junia”, “age”:31 }, { “id”:88, “name”:”Athene”, “age”:26 }, { “id”:89, “name”:”Veriee”, “age”:18 }, { “id”:90, “name”:”Rickie”, “age”:30 }, { “id”:91, “name”:”Carly”, “age”:23 }, { “id”:92, “name”:”Vern”, “age”:19 }, { “id”:93, “name”:”Trix”, “age”:26 }, { “id”:94, “name”:”Lenore”, “age”:20 }, { “id”:95, “name”:”Hanna”, “age”:30 }, { “id”:96, “name”:”Dominique”, “age”:21 }, { “id”:97, “name”:”Karlotta”, “age”:22 }, { “id”:98, “name”:”Levey”, “age”:20 }, { “id”:99, “name”:”Dalila”, “age”:18 }, { “id”:100, “name”:”Launce”, “age”:21 } ] Next, add a constructor in the SimplePagination component and set initial state as shown below − constructor(props) { super(props); this.state = { users: [], usersToBeShown: [], currentPage: 1 } }; Next, add componentDidMount lifecycle event and add below code to fetch and process the user information. componentDidMount() { fetch(”users.json”) .then((response) => response.json()) .then((data) => { // console.log(data); this.setState({ users: data, pageSize: 3, usersToBeShown: [], pageArray: [] }); this.calculatePaginationDetails(1) }); } Next, implement the pagination logic in the calculatePaginationDetails method as shown below − calculatePaginationDetails = (page) => { console.log(page) let users = this.state.users; let total = users.length; let pages = Math.floor((users.length / this.state.pageSize) + 1); let firstPage = 1;