react js Flashcards
What is react
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called “components”.
React Virtual DOM
DOM is basically the representation of the HTML code in a webpage. The document is the webpage itself, the objects are the HTML tags. And finally, the model of DOM is a tree structure:
What are the 3 benefit of Virtual DOM?
Each time you make a change in the code, DOM will be completely updated and rewritten. This is an expensive operation and consumes lots of time. In this point, React provides a solution: The Virtual DOM.
So when something changes:
1) React first creates an exact copy of the DOM
2) Then React figures out which part is new and only updates that specific part in the Virtual DOM3)Finally, React copies only the new parts of the Virtual DOM to the actual DOM, rather than completely rewriting it.
What is JSX
JSX (JavaScript XML) is a syntax extension to JavaScript used by React. JSX is basically used to write HTML tags inside JavaScript. Later, the JSX code will be translated into normal JavaScript, by Babel.
JSX class
className
Command to setup React scaffolding
npx create-react-app my-app
What is a React Component?
A component is an independent, reusable code block, which divides the UI into smaller pieces. In other words, we can think of components as LEGO blocks.
What are React 2 types of components
1) Functional (Stateless)
2) Class (Stateful).
Describe Functional (Stateless) Components
A functional component is basically a JavaScript (or ES6) function which returns a React element.
example: function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
So a React Functional Component: has these 3 things
1) is a JavaScript / ES6 function
2) must return a React element
3) take props as parameter if necessary
what is a Class (Stateful) Components
Class components are ES6 classes. They are more complex than functional components including constructors, life-cycle methods, render( ) function and state (data) management.
4 items React class component:
1) is an ES6 class, will be a component once it ‘extends’ React component.
2) can accept props (in the constructor) if needed
3) can maintain its own data with state
4 must have a render( ) method which returns a React element (JSX), or null
Create parentComponent file
import React, { Component } from ‘react’;
class ParentComponent extends Component { render() { return <h1>I'm the parent component.</h1>; } } export default ParentComponent;
Create firstChild file
import React from 'react'; // React needs to be imported const FirstChild = () => { return <p>I'm the 1st child!</p>; }; export default FirstChild;