FrontendDeveloper.in

React Interview Questions

  • Question 151

    What are the advantages of React over Vue.js?

    React has the following advantages over Vue.js:

    1. Gives more flexibility in large apps developing.
    2. Easier to test.
    3. Suitable for mobile apps creating.
    4. More information and solutions available.

    Note: The above list of advantages are purely opinionated and it vary based on the professional experience. But they are helpful as base parameters.

  • Question 152

    What is the difference between React and Angular?

    Let's see the difference between React and Angular in a table format.

    ReactAngular
    React is a library and has only the View layerAngular is a framework and has complete MVC functionality
    React handles rendering on the server sideAngularJS renders only on the client side but Angular 2 and above renders on the server side
    React uses JSX that looks like HTML in JS which can be confusingAngular follows the template approach for HTML, which makes code shorter and easy to understand
    React Native, which is a React type to build mobile applications are faster and more stableIonic, Angular's mobile native app is relatively less stable and slower
    In React, data flows only in one way and hence debugging is easyIn Angular, data flows both way i.e it has two-way data binding between children and parent and hence debugging is often difficult

    Note: The above list of differences are purely opinionated and it vary based on the professional experience. But they are helpful as base parameters.

  • Question 153

    Why React tab is not showing up in DevTools?

    When the page loads, React DevTools sets a global named __REACT_DEVTOOLS_GLOBAL_HOOK__, then React communicates with that hook during initialization. If the website is not using React or if React fails to communicate with DevTools then it won't show up the tab.

  • Question 154

    What are Styled Components?

    styled-components is a JavaScript library for styling React applications. It removes the mapping between styles and components, and lets you write actual CSS augmented with JavaScript.

  • Question 155

    Give an example of Styled Components?

    Lets create <Title> and <Wrapper> components with specific styles for each.

    import React from "react";
    import styled from "styled-components";
    
    // Create a <Title> component that renders an <h1> which is centered, red and sized at 1.5em
    const Title = styled.h1`
    font-size: 1.5em;
    text-align: center;
    color: palevioletred;
    `;
    
    // Create a <Wrapper> component that renders a <section> with some padding and a papayawhip background
    const Wrapper = styled.section`
    padding: 4em;
    background: papayawhip;
    `;
    

    These two variables, Title and Wrapper, are now components that you can render just like any other react component.

    <Wrapper>
    <Title>{"Lets start first styled component!"}</Title>
    </Wrapper>
    
  • Question 156

    What is Relay?

    Relay is a JavaScript framework for providing a data layer and client-server communication to web applications using the React view layer.

    Miscellaneous

  • Question 157

    What are the main features of Reselect library?

    Let's see the main features of Reselect library,

    1. Selectors can compute derived data, allowing Redux to store the minimal possible state.

    2. Selectors are efficient. A selector is not recomputed unless one of its arguments changes.

    3. Selectors are composable. They can be used as input to other selectors.

    4. Give an example of Reselect usage?

    Let's take calculations and different amounts of a shipment order with the simplified usage of Reselect:

    import { createSelector } from "reselect";
    
    const shopItemsSelector = (state) => state.shop.items;
    const taxPercentSelector = (state) => state.shop.taxPercent;
    
    const subtotalSelector = createSelector(shopItemsSelector, (items) =>
    items.reduce((acc, item) => acc + item.value, 0)
    );
    
    const taxSelector = createSelector(
    subtotalSelector,
    taxPercentSelector,
    (subtotal, taxPercent) => subtotal * (taxPercent / 100)
    );
    
    export const totalSelector = createSelector(
    subtotalSelector,
    taxSelector,
    (subtotal, tax) => ({ total: subtotal + tax })
    );
    
    let exampleState = {
    shop: {
    taxPercent: 8,
    items: [
    { name: "apple", value: 1.2 },
    { name: "orange", value: 0.95 },
    ],
    },
    };
    
    console.log(subtotalSelector(exampleState)); // 2.15
    console.log(taxSelector(exampleState)); // 0.172
    console.log(totalSelector(exampleState)); // { total: 2.322 }
    
  • Question 158

    Can Redux only be used with React?

    Redux can be used as a data store for any UI layer. The most common usage is with React and React Native, but there are bindings available for Angular, Angular 2, Vue, Mithril, and more. Redux simply provides a subscription mechanism which can be used by any other code.

  • Question 159

    Do you need to have a particular build tool to use Redux?

    Redux is originally written in ES6 and transpiled for production into ES5 with Webpack and Babel. You should be able to use it regardless of your JavaScript build process. Redux also offers a UMD build that can be used directly without any build process at all.

  • Question 160

    How Redux Form `initialValues` get updated from state?

    You need to add enableReinitialize : true setting.

    const InitializeFromStateForm = reduxForm({
    form: "initializeFromState",
    enableReinitialize: true,
    })(UserEdit);
    

    If your initialValues prop gets updated, your form will update too.

  • Question 161

    How React PropTypes allow different types for one prop?

    You can use oneOfType() method of PropTypes.

    For example, the height property can be defined with either string or number type as below:

    Component.propTypes = {
    size: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    };
    
  • Question 162

    Can I import an SVG file as react component?

    You can import SVG directly as component instead of loading it as a file. This feature is available with react-scripts@2.0.0 and higher.

    import { ReactComponent as Logo } from "./logo.svg";
    
    const App = () => (
    {/* Logo is an actual react component */}
    <Logo />
    );
    

    Note: Don't forget about the curly braces in the import.

  • Question 163

    What is render hijacking in react?

    The concept of render hijacking is the ability to control what a component will output from another component. It means that you decorate your component by wrapping it into a Higher-Order component. By wrapping, you can inject additional props or make other changes, which can cause changing logic of rendering. It does not actually enable hijacking, but by using HOC you make your component behave differently.

  • Question 164

    How to pass numbers to React component?

    We can pass numbers as props to React component using curly braces {} where as strings in double quotes "" or single quotes ''

    import React from "react";
    
    const ChildComponent = ({ name, age }) => {
    return (
    <>
    My Name is {name} and Age is {age}
    </>
    );
    };
    
    const ParentComponent = () => {
    return (
    <>
    <ChildComponent name="Chetan" age={30} />
    </>
    );
    };
    
    export default ParentComponent;
    
  • Question 165

    Do I need to keep all my state into Redux? Should I ever use react internal state?

    It is up to the developer's decision, i.e., it is developer's job to determine what kinds of state make up your application, and where each piece of state should live. Some users prefer to keep every single piece of data in Redux, to maintain a fully serializable and controlled version of their application at all times. Others prefer to keep non-critical or UI state, such as “is this dropdown currently open”, inside a component's internal state.

    Below are the rules of thumb to determine what kind of data should be put into Redux

    1. Do other parts of the application care about this data?
    2. Do you need to be able to create further derived data based on this original data?
    3. Is the same data being used to drive multiple components?
    4. Is there value to you in being able to restore this state to a given point in time (ie, time travel debugging)?
    5. Do you want to cache the data (i.e, use what's in state if it's already there instead of re-requesting it)?
Get LinkedIn Premium at Rs 399