Interview questions react Flashcards
What is React?
React is a front-end library developed by facebook. It allows for us to create really nice, reusable front-end UI components.
It can be used to develop complex/interactive Web and Mobile User Interfaces. It has a relatively massive amount of support.
What are some of the features of React?
- Uses virtual DOM instead of the actual DOM.
- Uses server-side rendering
- follows uni-directional flow for data binding
What are the limitations of React?
- It’s just a library, not a full framework.
- Its library is large and takes time to understand.
- It can be difficult for novice programmers.
- It can be complex with inline templating and JSX
What are some advantages of React?
- Increases application performance.
- Can be used on both client and server side.
- JSX increases code readability.
- Easy to integrate with other frameworks.
- Writing UI test cases is easy.
What is JSX?
JavaScript XML. Utilizes JavaScript expressiveness with HTML-like template syntax.
-Becomes really easy to understand
What is the virtual DOM? How does it work?
It is a lightweight JavaScript object which is just a copy of the real DOM. It treats the elements, attributes, and contents as Objects as their properties.
-The render function creates a node tree out of the React components. It updates the tree in response to mutations in data model.
What is the difference between the real and virtual DOM?
Real:
- Updates slow.
- Can directly update HTML
- Creates a new DOM if element updates.
- DOM manipulation is expensive.
- Too much memory wasted.
Virtual:
- Updates fast
- Cannot directly update HTML
- Updates JSX if element updates.
- DOM manipulation is easy.
- No memory waste.
Why can’t browsers read JSX?
Browsers can read JavaScript objects, but JSX is not a regular JavaScript object.
- To enable the browser to read JSX, we need to transform a JSX file to a JavaScript object using a transformer like Babel.
- After transforming, pass it to the browser.
How different is React’s ES6 syntax when compared to ES5?
1: Use import instead of require.
2: Use export instead of module.exports.
3: Extend component instead of using function to render a component.
4: props change.
5: State declared in constructor instead of getInitialState.
How is React different from angular?
React:
- Only the view of MVC
- Server side rendering
- Uses virtual DOM
- One-way data binding
- Compile time debugging
- Made by facebook
Angular:
- Complete MVC
- Client side rendering
- Uses real DOM
- Two-way data binding
- Run time debugging
- Made by Google
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.
Each React component must have a render() mandatorily. It returns a single React element which is the representation of the native DOM component. If more than one HTML element needs to be rendered, then they must be grouped together inside one enclosing tag such as , ,<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.</div>
How can you embed two or more components into one?
Place them both inside a form, group, div tag, etc.
What are props?
Props are shorthand for ‘properties’ in react. They are read-only and are immutable. Must be passed down from parent to child. Child CANNOT send a prop back to a parent.
What is state?
Are the source of data
Must be kept simple.
They are the objects which determine component rendering and behavior.
They are mutable, and can be used to create dynamic and interactive components.
Difference between state and props?
The parent component does not change the state value, while it does for props.
State changes inside of the actual component, while props change inside of the child components.
How can you update the state of a component?
this.setState();
What is the arrow function in React? How is it used?
They allow functions to bind the context of the components properly since in ES6 auto binding is not available by default.
Difference between stateful and stateless component?
Stateful:
- Stores info about state change in memory.
- Has authority to change state
- Contains knowledge of past, present, and future changes in state.
- Notified by stateless components about the requirement of the state change, and then send props down.
Stateless:
- Calculates internal state of components.
- Does not have authority to change state.
- Contains no knowledge of past, present, and future state changes.
- Receives props from stateful components and treat them as callbacks.
What are the three phases of React’s component’s lifecycle?
1: Initial Rendering Phase:
- Component is about to start its life journey and make its way to the DOM.
2: Updating Phase:
- Component is added to DOM, and it can get updated or re-rendered only when a prop or state change occurs.
3: Unmounting Phase:
- Component is destroyed and removed from DOM.