ReactJS - Tutorialspoint

3y ago
678 Views
284 Downloads
858.15 KB
13 Pages
Last View : 2d ago
Last Download : 3m ago
Upload by : Rosemary Rios
Transcription

ReactJS1

ReactJSAbout the TutorialReact is a front-end library developed by Facebook. It is used for handling the view layerfor web and mobile apps. ReactJS allows us to create reusable UI components. It iscurrently one of the most popular JavaScript libraries and has a strong foundation andlarge community behind it.AudienceThis tutorial will help JavaScript developers who look ahead to deal with ReactJS for thefirst time. We will try to introduce every concept by showing simple code examples thatcan be easily understood. After finishing all the chapters, you will feel confident workingwith ReactJS. As a bonus we will introduce additional elements that work well with ReactJSto help you learn the best practices and follow the modern JavaScript trends.PrerequisitesIf you want to work with ReactJS, you need to have solid knowledge of JavaScript,HTML5, and CSS. Even though ReactJS doesn't use HTML, the JSX is similar so your HTMLknowledge will be very helpful. We will explain this more in one of the next chapters. Wewill also use EcmaScript 2015 syntax so any knowledge in this area can be helpful.Copyright & Disclaimer Copyright 2017 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com2

ReactJSTable of ContentsAbout the Tutorial . 2Audience. 2Prerequisites. 2Copyright & Disclaimer . 2Table of Contents. 31. REACTJS OVERVIEW. 7React Features . 7React Advantages . 7React Limitations . 72. REACTJS ENVIRONMENT SETUP. 8Step 1 - Install Global Packages . 8Step 2 - Create the Root Folder . 8Step 3 - Add Dependencies and Plugins . 8Step 4 - Create the Files . 9Step 5 - Set Compiler, Server and Loaders. 9Step 6 - index.html. 10Step 7 - App.jsx and main.js . 11Step 8 - Running the Server. 123. REACTJS JSX . 13Using JSX. 13Nested Elements . 14Attributes . 15JavaScript Expressions . 15Styling . 173

ReactJSComments . 18Naming Convention . 194. REACTJS COMPONENTS . 20Stateless Example . 20Stateful Example . 225. REACTJS STATE . 256. REACTJS PROPS OVERVIEW . 27Using Props . 27Default Props . 28State and Props . 297. REACTJS PROPS VALIDATION . 32Validating Props. 328. REACTJS COMPONENT API . 35Set State . 35Force Update . 36Find Dom Node . 379. REACTJS COMPONENT LIFE CYCLE . 39Lifecycle Methods . 3910.REACTJS FORMS . 43Simple Example . 43Complex Example . 4411.REACTJS EVENTS . 47Simple Example . 47Child Events . 484

ReactJS12.REACTJS REFS . 51Using Refs . 5113.REACTJS KEYS . 53Using Keys . 5314.REACTJS ROUTER . 56Step 1 - Install a React Router . 56Step 2 - Create Components . 56Step 3 - Add a Router . 5815.REACTJS FLUX CONCEPT. 59Flux Elements . 59Flux Pros . 5916.REACTJS USING FLUX. 60Step 1 - Install Redux . 60Step 2 - Create Files and Folders . 60Step 3 - Actions . 60Step 4 - Reducers . 61Step 5 - Store . 62Step 6 - Root Component . 63Step 7 - Other Components. 6417.REACTJS ANIMATIONS. 66Step 1 - Install React CSS Transitions Group . 66Step 2 - Add a CSS file . 66Step 3 - Appear Animation . 66Step 4 - Enter and Leave Animations . 6818.REACTJS HIGHER ORDER COMPONENTS . 725

ReactJS19.REACTJS BEST PRACTICES . 746

1. ReactJS OverviewReactJSReactJS is JavaScript library used for building reusable UI components. According to Reactofficial documentation, following is the definition React is a library for building composable user interfaces. It encourages the creation ofreusable UI components, which present data that changes over time. Lots of people useReact as the V in MVC. React abstracts away the DOM from you, offering a simplerprogramming model and better performance. React can also render on the server usingNode, and it can power native apps using React Native. React implements one-wayreactive data flow, which reduces the boilerplate and is easier to reason about thantraditional data binding.React Features JSX JSX is JavaScript syntax extension. It isn't necessary to use JSX in Reactdevelopment, but it is recommended. Components React is all about components. You need to think of everything asa component. This will help you maintain the code when working on larger scaleprojects. Unidirectional data flow and Flux React implements one-way data flow whichmakes it easy to reason about your app. Flux is a pattern that helps keeping yourdata unidirectional. License React is licensed under the Facebook Inc. Documentation is licensedunder CC BY 4.0.React Advantages Uses virtual DOM which is a JavaScript object. This will improve apps performance,since JavaScript virtual DOM is faster than the regular DOM. Can be used on client and server side as well as with other frameworks. Component and data patterns improve readability, which helps to maintain largerapps.React Limitations Covers only the view layer of the app, hence you still need to choose othertechnologies to get a complete tooling set for development. Uses inline templating and JSX, which might seem awkward to some developers.7

2. ReactJS Environment SetupReactJSIn this chapter, we will show you how to set up an environment for successful Reactdevelopment. Notice that there are many steps involved but this will help speed up thedevelopment process later. We will need NodeJS, so if you don't have it installed, checkthe link from the following table.Sr.No.SoftwareDescription1NodeJS and NPMNodeJS is the platform needed for the Cordovadevelopment. Checkout our NodeJS Environment Setup.Step 1 - Install Global PackagesWe will need to install several packages for this setup. We will need some of the babelplugins, so let's first install babel by running the following code in the commandprompt window.C:\Users\username npm install -g babelC:\Users\username npm install -g babel-cliStep 2 - Create the Root FolderThe root folder will be named reactApp and we will place it on Desktop. After the folderis created, we need to open it and create empty package.json file inside by running npminit from the command prompt and follow the instructions.C:\Users\username\Desktop mkdir reactAppC:\Users\username\Desktop\reactApp npm initStep 3 - Add Dependencies and PluginsWe will use webpack bundler in these tutorial. Let's install webpack and webpack-devserver.C:\Users\username npm install webpack --saveC:\Users\username npm install webpack-dev-server --saveSince we want to use React, we need to install it first. The --save command will add thesepackages to package.json file.C:\Users\username\Desktop\reactApp npm install react --save8

ReactJSC:\Users\username\Desktop\reactApp npm install react-dom --saveAs already mentioned, we will need some babel plugins, so let's install it too.C:\Users\username\Desktop\reactApp npm install babel-coreC:\Users\username\Desktop\reactApp npm install babel-loaderC:\Users\username\Desktop\reactApp npm install pp npm install babel-preset-es2015Step 4 - Create the FilesLet's create several files that we need. It can be added manually or using the commandprompt.C:\Users\username\Desktop\reactApp touch index.htmlC:\Users\username\Desktop\reactApp touch App.jsxC:\Users\username\Desktop\reactApp touch main.jsC:\Users\username\Desktop\reactApp touch webpack.config.jsStep 5 - Set Compiler, Server and LoadersOpen webpack-config.js file and add the following code. We are setting webpack entrypoint to be main.js. Output path is the place where bundled app will be served. We arealso setting the development server to 8080 port. You can choose any port you want.And lastly, we are setting babel loaders to search for js files, and use es2015 and reactpresets that we installed before.webpack.config.jsvar config {entry: './main.js',output: {path:'./',filename: 'index.js',},devServer: {inline: true,port: 8080},module: {9

ReactJSloaders: [{test: /\.jsx? /,exclude: /node modules/,loader: 'babel',query: {presets: ['es2015', 'react']}}]}}module.exports config;Open the package.json and delete "test" "echo \"Error: no test specified\" && exit1" inside "scripts" object. We are deleting this line since we will not do any testing in thistutorial. Let's add the start command instead."start": "webpack-dev-server --hot"Now, we can use npm start command to start the server. --hot command will add livereload after something is changed inside our files so we don't need to refresh the browserevery time we change our code.Step 6 - index.htmlThis is just regular HTML. We are setting div id "app" as a root element for our appand adding index.js script, which is our bundled app file. !DOCTYPE html html lang "en" head meta charset "UTF-8" title React App /title /head body div id "app" /div script src "index.js" /script /body 10

ReactJS /html Step 7 - App.jsx and main.jsThis is the first React component. We will explain React components in depth in asubsequent chapter. This component will render Hello World!!!.App.jsximport React from 'react';class App extends React.Component {render() {return ( div Hello World!!! /div );}}export default App;We need to import this component and render it to our root App element, so we can seeit in the browser.main.jsimport React from 'react';import ReactDOM from 'react-dom';import App from './App.jsx';ReactDOM.render( App / , document.getElementById('app'));Note: Whenever you want to use something, you need to import it first. If you want tomake the component usable in other parts of the app, you need to export it after creationand import it in the file where you want to use it.11

ReactJSStep 8 - Running the ServerThe setup is complete and we can start the server by running the following command.C:\Users\username\Desktop\reactApp npm startIt will show the port we need to open in the browser. In our case, it ishttp://localhost:8080/. After we open it, we will see the following output.12

ReactJSEnd of ebook previewIf you liked what you saw Buy it from our store @ https://store.tutorialspoint.com13

React as the V in MVC. React abstracts away the DOM from you, offering a simpler programming model and better performance. React can also render on the server using Node, and it can power native apps using React Native. React implements one-way reactive data flow, which reduces the boilerplate and is easier to reason about than

Related Documents:

Python Django & React Js will be the best Choice. Complete full-stack web development course with Python - Django as backend and ReactJS in the frontend. Along with this, we will use a Payment Gateway and SMS Gateway in our E-Commerce Web and Other Applications. Eligibility: This Online Training Program on React Js - Django is for all students and

About the Tutorial React is a front-end library developed by Facebook. It is used for handling the view layer for web and mobile apps. ReactJS allows us to create reusable UI components. It is currently one of the most popular JavaScript libraries and has a strong foundation and large community behind it. Audience

MERN STACK WITH MODERN WEB PRACTICES Developers Connecting Application The main focus of the thesis was to learn and develop a full-stack web application using MERN stack (MongoDB, ExpressJS, ReactJS and NodeJS) with the use of modern practices. The thesis walks through the introduction and key concepts of MongoDB, ExpressJS, ReactJS,

tutorialspoint.com or google.com these are domain names. A domain name has two parts, TLD (Top Level Domain) and SLD (Second level domain), for example in tutorialspoint.com, tutorialspoint is second level domain of TLD .com, or you can say it's a subdomain of .com TLD. There are many top level domains available, like .com,

VueJS uses html, js and css separately. It is very easy for a beginner to understand and adopt the VueJS style. The template based approach for VueJS is very easy. React uses jsx approach. Everything is JavaScript for ReactJS. HTML and CSS are all part of JavaScript. Installation Tools React uses create react app and VueJS uses vue-cli /CDN/npm .

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the

tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the

Agile Development in a Medical Device Company Pieter Adriaan Rottier, Victor Rodrigues Cochlear Limited rrottier@cochlear.com.au Abstract This article discuss the experience of the software development group working in Cochlear with introducing Scrum as an Agile methodology. We introduce the unique challenges we faced due to the nature of our product and the medical device industry. These .