React Js Flashcards
What is React?
React is a Javascript library created by Facebook. It’s component based and used to create single page applications. Single page applications only load the application code once. You stay on the same page and the Javascript changes/updates the html or DOM to display new things without speaking to the server.
What are the features of React?
Some features of React are, component architecture, one way data flow, UI library, and Virtual Dom.
In calling setState, when would you pick one method of calling this function over the other?
You would use a function to set state when you need the current state or props of your component to calculate the next state. It is the most reliable to use the prevState function so you prevent the use of stale state or an “old” version of state that isn’t the current state. If you are certain you have the most current state or don’t need to the current state to calculate your setState then you can pass an object instead of a function.
Is setState a synchronous or async call?
setState is an asynchronous class. Meaning that it doesn’t update immediately and is batched. It will be updated on the next rerender of the component.
What are the different ways that you can call setState ?
You can set state by directly passing in an object to set with, or create a function that will copy the current state and updates based on the changes you make to it in your function.
List some of the major advantages of React.
Reusable components, Virtual DOM, React Developer Tools, faster rendering.
What are the limitations of React?
React focus on the view part of MVC i.e. UI of the web application. So, to overcome these that to focus on other development we need other tooling set technologies.
What is JSX?
JSX is a syntax extention of Javascript. It produces react elements for the virtual DOM. It represents objects in memory.
What is the virtual DOM? Explain how it works within ReactJS.
The DOM (Document Object Modal) is a programming interface for html and xml documents. It represents the page in a tree structure so that programs can read, access, and change the document structure, style and content. It can be modified with scripting language such as JavaScript.
The Virtual DOM is a lightweight representation of the DOM. It exists in memory but is never rendered, In React JSX tells the template compiler how to build in the virtual DOM tree. The Render() function creates the virtual DOM tree. When your app is updated (from things like setState) there will be two virtual DOM trees in memory. React will compare the two trees and compare the differences then reconcile them create a patch then render them to the real DOM. That way all the updates are batched and the DOM is only ever updated once in each update.
Why can’t browsers read JSX?
Browsers don’t have inherent implementation for the browsers engine to understand JSX objects. Because JSX objects aren’t JavaScript objects.
How different is React’s ES6 syntax when compared to ES5?
Using React with ES5:
Uses the React.createClass() API.
Defines component state using getInitialState().
Exports the component as a module using module.exports.
React with ES6:
Uses the ES6 class. Defines component state using this.state in the constructor(). Exports the component as a module using the export default. React with ES5:
Props are implicit.
Implicit binding of “this” to functions.
React with ES6:
Props are passed into the component via the constructor().
Explicit binding of “this” to functions in the constructor, using this.functionname.bind(this).
Differentiate between Real DOM and Virtual DOM.Real DOM vs Virtual DOM
There aren’t many major differences between the virtual and real dom. The virtual is an abstraction of the real DOM.
What do you understand from “In React, everything is a component.”
Components are the building blocks of a React application’s UI. These components split up the entire UI into small independent and reusable pieces. Then it renders each of these components independent of each other without affecting the rest of the UI.
Explain the purpose of render() in React.
Rendering is the React engine process walking through the virtual DOM and collecting the current state, props, structure, desired changes in the UI, etc.
The render function in react is used to create and update the UI. It describes how the ui should look on the browser window. It is part of the component life cycle and isn’t user called. It returns on JSX element that contains the view hierarchy for the current component. Render is called when: the react component is first instantiated following the constructor call, or after an update to the components props, or after a setState call.
How can you embed two or more components into one?
The components need to be put into a parent wrapper element. <div> or better use .</div>
What is Props?
Props are properties of components in react. They are used to pass data from parent component to child. Props are read only therefor should not be changed by the child component.
What is a state in React and how is it used?
State is a plain JavaScript object used by React to represent an information about the component’s current situation. The difference is while a “normal” variable “disappears” when their function exits, the state variables are preserved by React. It is used to enables a component to keep track of changing information in between renders and for it to be dynamic and interactive.
How can you update the state of a component?
You update the state of a component by calling the setState function.