1 Letβs mix some HTML and JS together JavaScript & React basics 2 Agenda History of Javascript - the old and the new What do we need to create a web UI? Tools and technologies New goodies in JavaScript Web UI key principles React basics - props, state, component lifecycle Project setup 3 React A JavaScript library for building user interfaces Uses Shadow DOM 2006-03-22 jQuery The most β€ and π© DOM manipulation library ES6 Big improvement to JS Prepared for many years Introduces a lot of new features 2013-05-29 2015-01-12 Babel Previously known as 6t5 babel Transpiles new JS to old one 2015-06-17 JavaScript Dark age ended like 10 years agoβ¦ Javascript practices - old and new 2016+ ES7 end ES next From now on ES committee creates every year new standard 1995 Brendan Eich Javascript practices - old and new 4 What do we need to create a web UI? β web browser (see the page & debug) β code editor (VS code) β programming language (JavaScript) β library to help us create the app (React) β package manager (NPM) β transpile new code features for older web browsers (Babel) β bundle JS in a single file (Webpack) β deploy the app (OpenShift, Surge) Javascript practices - old and new 5 https://en.wikipedia.org/wiki/Ajax_(programming) Ajax (Asynchronous JavaScript and XML) β Also known as XHR (XMLHttpRequest) β At first used to exchange XML documents (Extensible Markup Language) β Asynchronously exchange data with server β No longer needed to reload entire page just to get the data β Need for clever way how to identify user β Polling was used to update data on the fly Javascript practices - old and new 6 Single session β User logs in and is given a specific ID on server called session ID β User performs asynchronous operations β Once user is done, browser is closed and server resources are cleaned β Resources consuming β User has to log in every time new browser is opened (if application allows that) β Session hijacking (no need to know the password, just listen on specific events and get the session ID) Javascript practices - old and new 7 Single sign on/out β Transfers the need of tracking the log-in state from server to browser β Better user experience - no need to log in for each application β Allows you to login with social media and other providers β Allows user to automatically log out of all devices if there are some suspicions β Uses JWT and Oauth 2 β JWT - header, pyload and signature β Oauth 2 - protocol for authorization β Multiple services to take care of for us β Keycloak, Auth0 Javascript practices - old and new 8 https://www.w3counter.com/globalstats.php Internet explorer was the master β First there was Netscape β Internet explorer followed and gained a lot of traction because they were improving browser API and were the leading giants β IE used to had some quirks in order to support their paid customers and these changes were not adopted by other browsers because they were not standardized β Firefox was at that time heavily improving standards and adopting them β Opera and Chrome followed β Now Chrome has the leading (63.56%) followed by Safari (19.85%) with IE and Edge on third place (5.43%) Javascript practices - old and new 9 jQuery β Easy to understand wrapper around JavaScript β In its prime, it was the most used JavaScript library β People could program in jQuery, not JavaScript β To this date, for many people it is still a go to library when they need some quick prototype β With the evolution of the language β$β is becoming obsolete β And here is why: el.classList.add('show'); el.classList.remove('hide'); .show { transition: opacity 400ms; } .hide { opacity: 0; } $(el).fadeIn(); Javascript practices - old and new 10 New vs old Javascript practices - old and new 11 New is always better! Javascript practices - old and new 12 Variables and their scoping - old and new β Global - var β Variable leaking β Variable overriding β Local scope - let and const (new way) β let - can be changes, meaning its data type and value can be changed β const - canβt be changed, meaning its data type canβt be changed but inner value can const myVariable = βhelloβ; // error! myVariable = βsomethingβ; const otherVar = { foo: βbarβ }; // correct! otherVar.foo = βbazβ; Javascript practices - old and new 13 Callback hell β A way how to execute some code after asynchronous action happened β Chain functions together β Choose function based on some conditions Javascript practices - old and new 14 Callback hell β A way how to execute some code after asynchronous action happened β Chain functions together β Choose function based on some conditions Javascript practices - old and new 15 Callback hell β A way how to execute some code after asynchronous action happened β Chain functions together β Choose function based on some conditions // first we must get user by id const hotcakes = await CallEndpoint('/api/getidbyusername/hotcakes' ); const followers = await CallEndpoint('/api/getfollowersbyid' ); const other = await CallEndpoint('/api/someothercall' ); const another = await CallEndpoint('/api/someothercall' ); const andOther = await CallEndpoint('/api/someothercall' ); Javascript practices - old and new 16 Transpilers β Transpiler is a tool, that takes a code written in one language and transforms it into a different language (or different version in some cases) β Due to browser backwards compatibility we canβt just use the latest features of JS. They would not work. β Code has to be transformed into something browsers understand β There are also many variations of the language that will never be supported in browsers. Yet we want to use them. β JSX β Typescript β JS is becoming more and more compiled language β Many speak of an βAge of a Transpilersβ Javascript practices - old and new 17 Transpilers β Transpiler is a tool, that takes a code written in one language and transforms it into a different language (or different version in some cases) β Due to browser backwards compatibility we canβt just use the latest features of JS. They would not work. β Code has to be transformed into something browsers understand β There are also many variations of the language that will never be supported in browsers. Yet we want to use them. β JSX β Typescript β JS is becoming more and more compiled language β Many speak of an βAge of a Transpilersβ Javascript practices - old and new 18 Type checkers β Typechecker is a tool to help developers keep code stable by introducing types β Since many languages (JavaScript included) have no types it can be hard to do complex features Javascript practices - old and new 19 Type checkers - Typescript β > TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. β Created by Microsoft, very similar to c# β First introduction to classes - JavaScript has no understanding of classes, even new classes are just syntax sugar β Adds option to private and public properties β Generic types β Really powerful, but hard to understand β Type checking when building your application Javascript practices - old and new 20 JS is slowly moving towards functional programming β With a release of ES6 (ES2015) JS specification the language drastically shifted towards functional programming β Introduction of lambda functions, many many new prototype functions β Prototypes (Array, Object, Map, Set, ...) received a huge upgrade when it comes to natively supported functions which were previously only possible thanks to certain libraries. (Map, Reduce, Entries, ...) β There are whole frameworks based on FP and shifting away from OOP β There are also many JS extensions which are based entirely upon FP β RxJS, CircleJS, LodashFP, Ramda β Can fully replace Objects with closures and composition Although there are some who donβt like this that much Javascript practices - old and new 21 https://www.ecma-international.org/ Improved Browser API β Browsers (not IE) have transformed significantly in a past few years β Most of the are actually following and keeping up with ECMA (Javascript) specifications β Many JS utility libraries are becoming obsolete because browsers support is getting better and better β Browsers support I18N (Internationalization and localization) natively β And many more β Browser plugins β Security β Performance β Debugging tools Javascript practices - old and new 22 Build tools - module based codebase β Script tags are the history, now you need only one and the rest will be loaded on demand β Move from CommonJS to MJS β You can now actually reference a different module from JS file in a browser?!. (import/export) β We no longer need global variables everywhere β JS is sadly becoming compiled language (Thanks IE) β Thanks to modules, code splitting is becoming really simple and easy β Scripts are no longer loaded all at once β They can be loaded on demand β Huge performance and also UX improvements Javascript practices - old and new 23 Dependency headache β Thanks to the explosion of JS in recent years, your bundles will have thousands of external dependencies β Libraries depend on libraries which depend on libraries of libraries β After installing React, ReactDOM and Patternfly you will have over 100 external dependencies β At larger scale, any breaking change to a single dependency may cause the whole project collapse and requires extreme caution β Any upgrade must be considered and planned and will likely require tons of refactoring β YOU DONβT NEED LIBRARY FOR EVERYTHING β Some libraries has become so popular that everybody uses them and you can end up with multiple copies of the same code but in different versions Javascript practices - old and new 24 NPM vs Yarn NPM β Created by Google β Included by default with node β Finally supports monorepo (from version 7) β Hosts over 1.3 mil of packages β Option to audit your packages Yarn β Created by Facebook β First introduction of lock file β First introduction of monorepo β Mirror of all npm packages β Yarn 2.0 adds plgβnβplay - option to minimize download of packages on install Javascript practices - old and new 25 Web UI key principles (React based) 1. Component-Based Approach β UI is divided into reusable pieces of code called components (like a LEGO) β Components can have a hierarchy, allowing the creation of complex user interfaces from simple parts. 2. JSX (JavaScript XML) β Syntax similar to HTML, used in React to describe what the UI should look like. β During compilation, JSX is transformed into JavaScript function calls. Javascript practices - old and new 26 Web UI key principles (React based) 3. One-Way Data Binding β Data can be passed to components via read-only props. β Components can have their own internal state, which can change. β When the state changes, the component re-renders to reflect it. 4. Virtual DOM β It is a lightweight representation of the actual DOM. β Minimizes the number of updates made to the actual DOM by batching them together. It only updates parts of the DOM that have changed, rather than re-rendering the entire page. 5. Declarative Approach β Instead of telling the system step-by-step how to change the UI, React describes what the final state should look like. 6. State Management β In smaller applications, state management can be handled directly within components, while in larger applications, libraries like Redux or Context API are often used to help manage global state. React beginner 27 https://reactjs.org/ What is React? Definition Declarative, component based JavaScript library for building UI β Components are (sometimes) stand alone units with well defined API. β You donβt need to know how the component works under the hood to be able to use it. β React is build on rather simple principles which allow developers build complex solutions to satisfy their customers needs. React IS NOT: β state management library β data fetching library β data visualization library β framework React beginner 28 https://www.youtube.com/playlist?list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d React tutorials React beginner 29 Component and props β Component is a stand alone UI piece with well defined API β API can provide a set of props (you can send multiple props to single component) β Props is an object β Props are given to component from its parent β Prop can be required or optional β Key is a prop name, and value is its value β Prop is a variable which is consumed and used by the component β Component will most likely throw an error if its missing a required props β Prop requirement and props can be defined via propTypes β PropTypes is a static attribute on a component type β Each prop type definition is a function that checks the prop content React beginner 30 Props manipulating β There are two types of props β Values - passing data down β Callbacks - user (or async) interactions β Use spread to control multiple props β Combine them together β Spread them to pass them down const InputComponent = ({ onChange, ...props }) => { return ( onChange(event.target.value )} /> ) } React beginner 31 Component props examples const TitleComponent = props => { const {text, size} = props; // ES6 destructuring return (