React Flashcards
What is React?
React is a 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.
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 JSX?
It provides syntactic sugar for the React.createElement() function. It’s used with React to describe what the UI should look like. JSX produces React “elements”. JSX is the return mark up output for class and function components.
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.
The object representation of React Element would be as follows:
const element = React.createElement( 'div', {id: 'login-btn'}, 'Login' )
The above React.createElement() function returns an object:
{ type: 'div', props: { children: 'Login', id: 'login-btn' } }
Elements are what components are “made of” A component can be a class with a render() method or it can be defined as a function. It takes props as an input, and returns a JSX tree as the output.
How to create components in React?
Function Components: 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.
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 won’t compare current props and state. The component will re-render by default whenever shouldComponentUpdate is called.
What is state in React?
State of a component holds data that may change over the lifetime of the component.
State is private and fully controlled by the component ,i.e., it is not accessible to any other component until the owner component decides to pass it.
What are props in React?
Props are inputs to components. 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.
- Inform about state changes.
- Use via props.reactProp inside component’s render() method.
What is the difference between state and props?
Both props and state are plain JavaScript objects. Both of them hold information that influences the output of render.
Props get passed TO the component similar to function parameters and are read-only
State is managed WITHIN the component similar to variables declared within a function.
Props are a way of passing data from parent to child. State is reserved only for interactivity, that is, data that changes over time.
Why should we not update the state directly?
If you try to update the state directly then it won’t re-render the component.
Instead use setState() method. It schedules an update to a component’s state object. When state changes, the component responds by re-rendering.
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.
setState({ name: ‘John’ }, () => console.log(‘The name has updated and component re-rendered’))
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
- In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name.
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.
- 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.
- Arrow functions in callbacks: You can use arrow functions directly in the callbacks.
Note: If the callback is passed as prop to child components, those components might do an extra re-rendering.
JSX Represents Objects
Babel compiles JSX down to React.createElement() calls.
React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object
These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen.
React reads these objects and uses them to construct the DOM and keep it up to date.
What are react elements?
Elements are the smallest building blocks of React apps.
An element describes what you want to see on the screen
Unlike browser DOM elements, React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.