React.js

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What are controlled components?

A component that controls the input elements within the forms on subsequent user input is called Controlled Component, i.e, every state mutation will have an associated handler function. For example, to write all the names in uppercase letters, we use handleChange as below,

What is React Fiber?

Fiber is the new reconciliation engine or reimplementation of core algorithm in React v16. The goal of React Fiber is to increase its suitability for areas like animation, layout, gestures, ability to pause, abort, or reuse work and assign priority to different types of updates; and new concurrency primitives.

What are fragments?

It's a common pattern in React which is used for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM. return ( ) There is also a shorter syntax, but it's not supported in many tools: return ( )

What are synthetic events in React?

SyntheticEvent is a cross-browser wrapper around the browser's native event. Its API is same as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

What is the difference between Shadow DOM and Virtual DOM?

The Shadow DOM is a browser technology designed primarily for scoping variables and CSS in web components. The Virtual DOM is a concept implemented by libraries in JavaScript on top of browser APIs.

How Virtual DOM works?

The Virtual DOM works in three simple steps. Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation. vdom Then the difference between the previous DOM representation and the new one is calculated. vdom2 Once the calculations are done, the real DOM will be updated with only the things that have actually changed. vdom3

What are the major features of React?

The major features of React are: It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive. Supports server-side rendering. Follows Unidirectional data flow or data binding. Uses reusable/composable UI components to develop the view.

What is the use of refs?

The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.

What are portals in React?

Portal is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. ReactDOM.createPortal(child, container) The first argument is any render-able React child, such as an element, string, or fragment. The second argument is a DOM element.

What are stateless components?

If the behaviour is independent of its state then it can be a stateless component. You can use either a function or a class for creating stateless components. But unless you need to use a lifecycle hook in your components, you should go for function components. There are a lot of benefits if you decide to use function components here; they are easy to write, understand, and test, a little faster, and you can avoid the this keyword altogether.

What are stateful components?

If the behaviour of a component is dependent on the state of the component then it can be termed as stateful component. These stateful components are always class components and have a state that gets initialized in the constructor. super(props) // ... React 16.8 Update: Hooks let you use state and other React features without writing classes. The Equivalent Functional Component const [count, setCount] = useState(0); return ( // JSX )

Which is preferred option with in callback refs and findDOMNode()?

It is preferred to use callback refs over findDOMNode() API. Because findDOMNode() prevents certain improvements in React in the future. The legacy approach of using findDOMNode: findDOMNode(this).scrollIntoView() The recommended approach is: super(props); this.node = createRef(); this.node.current.scrollIntoView();

What is the difference between createElement and cloneElement?

JSX elements will be transpiled to React.createElement() functions to create React elements which are going to be used for the object representation of UI. Whereas cloneElement is used to clone an element and pass it new props.

What are forward refs?

Ref forwarding is a feature that lets some components take a ref they receive, and pass it further down to a child. )); // Create ref to the DOM button: const ref = React.createRef();

What is state in React?

State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components. Let's create a user component with message state, super(props) message: 'Welcome to React world' return ( ) state State is similar to props, but it is private and fully controlled by the component ,i.e., it is not accessible to any other component till the owner component decides to pass it.

What is the purpose of using super constructor with props argument?

A child class constructor cannot make use of this reference until the super() method has been called. The same applies to ES6 sub-classes as well. The main reason for passing props parameter to super() call is to access this.props in your child constructors. Passing props: super(props) Not passing props: super() console.log(this.props) // prints undefined // but props parameter is still available // no difference outside constructor The above code snippets reveals that this.props is different only within the constructor. It would be the same outside the constructor.

What is "key" prop and what is the benefit of using it in arrays of elements?

A key is a special string attribute you should include when creating arrays of elements. Key prop helps React identify which items have changed, are added, or are removed. Most often we use ID from our data as key: ) When you don't have stable IDs for rendered items, you may use the item index as a key as a last resort: ) Note: Using indexes for keys is not recommended if the order of items may change. This can negatively impact performance and may cause issues with component state. If you extract list item as separate component then apply keys on list component instead of li tag. There will be a warning message in the console if the key prop is not present on list items.

What is the difference between Element and Component?

An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated. The object representation of React Element would be as follows: const element = React.createElement( 'div', 'Login' ) The above React.createElement() function returns an object: type: 'div', children: 'Login', id: 'login-btn' And finally it renders to the DOM using ReactDOM.render(): Whereas a component can be declared in several different ways. It can be a class with a render() method or it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output: Then JSX gets transpiled to a React.createElement() function tree: 'div', 'Login' )

What are the lifecycle methods of React?

Before React 16.3 componentWillMount: Executed before rendering and is used for App level configuration in your root component. componentDidMount: Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur. componentWillReceiveProps: Executed when particular prop updates to trigger state transitions. shouldComponentUpdate: Determines if the component will be updated or not. By default it returns true. If you are sure that the component doesn't need to render after state or props are updated, you can return false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives new prop. componentWillUpdate: Executed before re-rendering the component when there are props & state changes confirmed by shouldComponentUpdate() which returns true. componentDidUpdate: Mostly it is used to update the DOM in response to prop or state changes. componentWillUnmount: It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component. React 16.3+ getDerivedStateFromProps: Invoked right before calling render() and is invoked on every render. This exists for rare use cases where you need a derived state. Worth reading if you need derived state. componentDidMount: Executed after first rendering and where all AJAX requests, DOM or state updates, and set up event listeners should occur. shouldComponentUpdate: Determines if the component will be updated or not. By default, it returns true. If you are sure that the component doesn't need to render after the state or props are updated, you can return a false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives a new prop. getSnapshotBeforeUpdate: Executed right before rendered output is committed to the DOM. Any value returned by this will be passed into componentDidUpdate(). This is useful to capture information from the DOM i.e. scroll position. componentDidUpdate: Mostly it is used to update the DOM in response to prop or state changes. This will not fire if shouldComponentUpdate() returns false. componentWillUnmount It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.

What is the difference between HTML and React event handling?

Below are some of the main differences between HTML and React event handling, In HTML, the event name usually represents in lowercase as a convention: Whereas in React it follows camelCase convention: In HTML, you can return false to prevent default behavior: Whereas in React you must call preventDefault() explicitly: event.preventDefault() console.log('The link was clicked.') In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name. (refer "activateLasers" function in the first point for example)

Why fragments are better than container divs?

Below are the list of reasons, Fragments are a bit faster and use less memory by not creating an extra DOM node. This only has a real benefit on very large and deep trees. Some CSS mechanisms like Flexbox and CSS Grid have a special parent-child relationships, and adding divs in the middle makes it hard to keep the desired layout. The DOM Inspector is less cluttered.

What is the difference between state and props?

Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props get passed to the component similar to function parameters whereas state is managed within the component similar to variables declared within a function.

What is children prop?

Children is a prop (this.props.children) that allows you to pass components as data to other components, just like any other prop you use. Component tree put between component's opening and closing tag will be passed to that component as children prop. There are several methods available in the React API to work with this prop. These include React.Children.map, React.Children.forEach, React.Children.count, React.Children.only, React.Children.toArray. A simple usage of children prop looks as below, ReactDOM.render( node )

What is context?

Context provides a way to pass data through the component tree without having to pass props down manually at every level. For example, authenticated users, locale preferences, UI themes need to be accessed in the application by many components.

When to use a Class Component over a Function Component?

If the component needs state or lifecycle methods then use class component otherwise use function component. However, from React 16.8 with the addition of Hooks, you could use state , lifecycle methods and other features that were only available in class component right in your function component. *So, it is always recommended to use Function components, unless you need a React functionality whose Function component equivalent is not present yet, like Error Boundaries *

How to set state with a dynamic key name?

If you are using ES6 or the Babel transpiler to transform your JSX code then you can accomplish this with computed property names.

Why should we not update the state directly?

If you try to update the state directly then it won't re-render the component. //Wrong this.state.message = 'Hello world' Instead use setState() method. It schedules an update to a component's state object. When state changes, the component responds by re-rendering. //Correct Note: You can directly assign to the state object either in constructor or using latest javascript's class field declaration syntax.

What is JSX?

JSX is a XML-like syntax extension to ECMAScript (the acronym stands for JavaScript XML). Basically it just provides syntactic sugar for the React.createElement() function, giving us expressiveness of JavaScript along with HTML like template syntax. return( )

Is lazy function supports named exports?

No, currently React.lazy function supports default exports only. If you would like to import modules which are named exports, you can create an intermediate module that reexports it as the default. It also ensures that tree shaking keeps working and don’t pull unused components. Let's take a component file which exports multiple named components, // MoreComponents.js export const SomeComponent = /* ... */; export const UnusedComponent = /* ... */; and reexport MoreComponents.js components in an intermediate file IntermediateComponent.js // IntermediateComponent.js Now you can import the module using lazy function as below,

What are Higher

Order Components?-A higher-order component (HOC) is a function that takes a component and returns a new component. Basically, it's a pattern that is derived from React's compositional nature. We call them pure components because they can accept any dynamically provided child component but they won't modify or copy any behavior from their input components. const EnhancedComponent = higherOrderComponent(WrappedComponent) HOC can be used for many use cases: Code reuse, logic and bootstrap abstraction. Render hijacking. State abstraction and manipulation. Props manipulation.

What are props in React?

Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component. The primary purpose of props in React is to provide following component functionality: Pass custom data to your component. Trigger state changes. Use via this.props.reactProp inside component's render() method. For example, let us create an element with reactProp property: This reactProp (or whatever you came up with) name then becomes a property attached to React's native props object which originally already exists on all components created using React library. props.reactProp

What is React?

React is an open-source front-end JavaScript library that is used for building user interfaces, especially for single-page applications. It is used for handling view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook's News Feed in 2011 and on Instagram in 2012.

What are Pure Components?

React.PureComponent is exactly the same as React.Component except that it handles the shouldComponentUpdate() method for you. When props or state changes, PureComponent will do a shallow comparison on both props and state. Component on the other hand won't compare current props and state to next out of the box. Thus, the component will re-render by default whenever shouldComponentUpdate is called.

What are uncontrolled components?

The Uncontrolled Components are the ones that store their own state internally, and you query the DOM using a ref to find its current value when you need it. This is a bit more like traditional HTML. In the below UserProfile component, the name input is accessed using ref. super(props) this.handleSubmit = this.handleSubmit.bind(this) this.input = React.createRef() alert('A name was submitted: ' + this.input.current.value) event.preventDefault() return ( ); In most cases, it's recommend to use controlled components to implement forms. In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.

What is Virtual DOM?

The Virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the "real" DOM. It's a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

What is the purpose of callback function as an argument of setState()?

The callback function is invoked when setState finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action. Note: It is recommended to use lifecycle method rather than this callback function.

How to write comments in React?

The comments in React/JSX are similar to JavaScript Multiline comments but are wrapped in curly braces. Single-line comments: Multi-line comments:

What are the different phases of component lifecycle?

The component lifecycle has three distinct lifecycle phases: Mounting: The component is ready to mount in the browser DOM. This phase covers initialization from constructor(), getDerivedStateFromProps(), render(), and componentDidMount() lifecycle methods. Updating: In this phase, the component gets updated in two ways, sending the new props and updating the state either from setState() or forceUpdate(). This phase covers getDerivedStateFromProps(), shouldComponentUpdate(), render(), getSnapshotBeforeUpdate() and componentDidUpdate() lifecycle methods. Unmounting: In this last phase, the component is not needed and gets unmounted from the browser DOM. This phase includes componentWillUnmount() lifecycle method. It's worth mentioning that React internally has a concept of phases when applying changes to the DOM. They are separated as follows Render The component will render without any side effects. This applies to Pure components and in this phase, React can pause, abort, or restart the render. Pre-commit Before the component actually applies the changes to the DOM, there is a moment that allows React to read from the DOM through the getSnapshotBeforeUpdate(). Commit React works with the DOM and executes the final lifecycles respectively componentDidMount() for mounting, componentDidUpdate() for updating, and componentWillUnmount() for unmounting. React 16.3+ Phases (or an interactive version) phases 16.4+ Before React 16.3 phases 16.2

What is the main goal of React Fiber?

The goal of React Fiber is to increase its suitability for areas like animation, layout, and gestures. Its headline feature is incremental rendering: the ability to split rendering work into chunks and spread it out over multiple frames. from documentation Its main goals are: Ability to split interruptible work in chunks. Ability to prioritize, rebase and reuse work in progress. Ability to yield back and forth between parents and children to support layout in React. Ability to return multiple elements from render(). Better support for error boundaries.

How to bind methods or event handlers in JSX callbacks?

There are 3 possible ways to achieve this: Binding in Constructor: In JavaScript classes, the methods are not bound by default. The same thing applies for React event handlers defined as class methods. Normally we bind them in constructor. super(props); this.handleClick = this.handleClick.bind(this); console.log('Click happened'); Public class fields syntax: If you don't like to use bind approach then public class fields syntax can be used to correctly bind callbacks. console.log('this is:', this) Arrow functions in callbacks: You can use arrow functions directly in the callbacks. console.log('Click happened'); Note: If the callback is passed as prop to child components, those components might do an extra re-rendering. In those cases, it is preferred to go with .bind() or public class fields syntax approach considering performance.

How to create refs?

There are two approaches This is a recently added approach. Refs are created using React.createRef() method and attached to React elements via the ref attribute. In order to use refs throughout the component, just assign the ref to the instance property within constructor. super(props) this.myRef = React.createRef() You can also use ref callbacks approach regardless of React version. For example, the search bar component's input element is accessed as follows, super(props); this.txtSearch = null; this.txtSearch = e; return ( ); You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach.

How to create components in React?

There are two possible ways to create a component. Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as the first parameter and return React elements: Class Components: You can also use ES6 class to define a component. The above function component can be written as:

What is reconciliation?

When a component's props or state change, React decides whether an actual DOM update is necessary by comparing the newly returned element with the previously rendered one. When they are not equal, React will update the DOM. This process is called reconciliation.

What is Lifting State Up in React?

When several components need to share the same changing data then it is recommended to lift the shared state up to their closest common ancestor. That means if two child components share the same data from its parent, then move the state to parent instead of maintaining local state in both of the child components.

How to create props proxy for HOC component?

You can add/edit props passed to the component using props proxy pattern like this: title: 'New Header', footer: false, showFeatureX: false, showFeatureY: true

How to pass a parameter to an event handler or callback?

You can use an arrow function to wrap around an event handler and pass parameters: This is an equivalent to calling .bind: Apart from these two approaches, you can also pass arguments to a function which is defined as arrow function console.log("Hello, your ticket number is", id)

What are inline conditional expressions?

You can use either if statements or ternary expressions which are available from JS to conditionally render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator &&. : You don't have unread messages.

What would be the common mistake of function being called every time the component renders?

You need to make sure that function is not being called while passing the function as a parameter. // Wrong: handleClick is called instead of passed as a reference! Instead, pass the function itself without parenthesis: // Correct: handleClick is passed as a reference!

Why React uses className over class attribute?

class is a keyword in JavaScript, and JSX is an extension of JavaScript. That's the principal reason why React uses className instead of class. Pass a string as the className prop.


Ensembles d'études connexes

Med/Surg Prep U ch. 39, 40, 41, 42

View Set

WWU MKTG 380 Midterm 1 Questions

View Set

Chapter 21: Respiratory Care Modalities

View Set

ch. 9 data privacy and confidentiality

View Set