General question Flashcards
A === B
a and b are equal in both value and type
A == B
a and b are equal in value alone
What is closure
Closure is the combination of a function bundled together (enclosed) with references to its surrounding state(the lexical environment). In Javascript , closures are created every time a function is created, at function creation time.
What is a promise
A promise is an object that may produce a single value sometime in the future. Either resolved or unresolved
This KEYWORD
refers to the object it belongs to. It’s set with bind()
What is SASS
its a css preprocessor. Gives you room to access variables and functions. You can nest and let more. It compiles the code.
403
UNAUTHORIZED REQUEST
404
NOT FOUND
201
RESOURCE RECREATED
400
CLIENT ERROR/BAD REQUEST
500
SERVER ERROR
What is the difference between the Virtual DOM and the real DOM
Diffing….the virtual dom looks at the state and the next state of the dom and say oh this is what I am to change..
Shadow DOM
Its a browser specific technology.
JSX
1) in full, it’s called Javascript Xml
2) write javascript with an html-like template syntax(not HTML, not a string)
3) produces elements that represent objects
What is the difference between an element and a component?
A component is a function that returns an element.
Element is created by jsx as an object.
You can create react without jsx.
Like react.createElement(‘div’,,null,’hello’)
How to pass a value from a child to parent?
Functional props. The parent will pass functional props to child then the child will call the function then pass it to the parent.
What is the difference between props and state?
Props is passed through a component.
State is manage in a given component. If you need it in another component, you will have to pass it as props.
What is a component lifecycle
1) mounting - componentdidmount
2) updating - componentdidupdate
3) unmounting - componentwillunmount
How do you update the lifecycle in function components?
UseEffect
What is React?
React is an open-source frontend JavaScript library which 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 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?
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.
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', {id: 'login-btn'}, 'Login' )
The above React.createElement() function returns an object:
{ type: 'div', props: { children: 'Login', id: 'login-btn' } }
And finally it renders to the DOM using ReactDOM.render():
<div>Login</div>
Whereas a component can be declared in several different ways. It can be a class with a render() method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:
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 first parameter and return React elements: function Greeting({ message }) { return <h1>{`Hello, ${message}`}</h1>
}
Class Components: You can also use ES6 class to define a component. The above function component can be written as: class Greeting extends React.Component { render() { return <h1>{`Hello, ${this.props.message}`}</h1> } }
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.
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 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.
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.
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.
Why should we not update the state directly?
If you try to update 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.
What is the difference 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:
<a></a>
Whereas in React you must call preventDefault() explicitly:
function handleClick(event) { event.preventDefault() console.log('The link was clicked.') } 3. 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)
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.
class Component extends React.Component { constructor(props) { super(props) this.handleClick = this.handleClick.bind(this) }
handleClick() { // ... } }
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.
handleClick = () => {
console.log(‘this is:’, this)
}
{‘Click me’}
Arrow functions in callbacks: You can use arrow functions directly in the callbacks.
this.handleClick(event)}>
{‘Click me’}
What are synthetic events in React?
SyntheticEvent is a cross-browser wrapper around the browser’s native event. It’s API is same as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
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 &&.
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.
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.
How to create refs?
React.createRef(). useRef hook