FrontendDeveloper.in

React Interview Questions

  • Question 226

    What is formik?

    Formik is a small react form library that helps you with the three major problems,

    1. Getting values in and out of form state
    2. Validation and error messages
    3. Handling form submission
  • Question 228

    Do browsers understand JSX code?

    No, browsers can't understand JSX code. You need a transpiler to convert your JSX to regular Javascript that browsers can understand. The most widely used transpiler right now is Babel.

  • Question 229

    Describe about data flow in react?

    React implements one-way reactive data flow using props which reduce boilerplate and is easier to understand than traditional two-way data binding.

  • Question 230

    What is MobX?

    MobX is a simple, scalable and battle tested state management solution for applying functional reactive programming (TFRP). For ReactJS application, you need to install below packages,

    npm install mobx --save
    npm install mobx-react --save
    
  • Question 231

    What are the differences between Redux and MobX?

    Below are the main differences between Redux and MobX,

    TopicReduxMobX
    DefinitionIt is a javascript library for managing the application stateIt is a library for reactively managing the state of your applications
    ProgrammingIt is mainly written in ES6It is written in JavaScript(ES5)
    Data StoreThere is only one large store exist for data storageThere is more than one store for storage
    UsageMainly used for large and complex applicationsUsed for simple applications
    PerformanceNeed to be improvedProvides better performance
    How it storesUses JS Object to storeUses observable to store the data
  • Question 232

    Should I learn ES6 before learning ReactJS?

    No, you don’t have to learn es2015/es6 to learn react. But you may find many resources or React ecosystem uses ES6 extensively. Let's see some of the frequently used ES6 features,

    1. Destructuring: To get props and use them in a component
    // in es 5
    var someData = this.props.someData;
    var dispatch = this.props.dispatch;
    
    // in es6
    const { someData, dispatch } = this.props;
    
    1. Spread operator: Helps in passing props down into a component
    // in es 5
    <SomeComponent someData={this.props.someData} dispatch={this.props.dispatch} />
    
    // in es6
    <SomeComponent {...this.props} />
    
    1. Arrow functions: Makes compact syntax
    // es 5
    var users = usersList.map(function (user) {
    return <li>{user.name}</li>;
    });
    // es 6
    const users = usersList.map((user) => <li>{user.name}</li>);
    
  • Question 233

    What is Concurrent Rendering?

    The Concurrent rendering makes React apps to be more responsive by rendering component trees without blocking the main UI thread. It allows React to interrupt a long-running render to handle a high-priority event. i.e, When you enabled concurrent Mode, React will keep an eye on other tasks that need to be done, and if there's something with a higher priority it will pause what it is currently rendering and let the other task finish first. You can enable this in two ways,

    // 1. Part of an app by wrapping with ConcurrentMode
    <React.unstable_ConcurrentMode>
    <Something />
    </React.unstable_ConcurrentMode>;
    
    // 2. Whole app using createRoot
    ReactDOM.unstable_createRoot(domNode).render(<App />);
    
  • Question 234

    What is the difference between async mode and concurrent mode?

    Both refers the same thing. Previously concurrent Mode being referred to as "Async Mode" by React team. The name has been changed to highlight React’s ability to perform work on different priority levels. So it avoids the confusion from other approaches to Async Rendering.

  • Question 235

    Can I use javascript urls in react16.9?

    Yes, you can use javascript: URLs but it will log a warning in the console. Because URLs starting with javascript: are dangerous by including unsanitized output in a tag like <a href> and create a security hole.

    const companyProfile = {
    website: "javascript: alert('Your website is hacked')",
    };
    // It will log a warning
    

    Remember that the future versions will throw an error for javascript URLs.

  • Question 236

    What is the purpose of eslint plugin for hooks?

    The ESLint plugin enforces rules of Hooks to avoid bugs. It assumes that any function starting with ”use” and a capital letter right after it is a Hook. In particular, the rule enforces that,

    1. Calls to Hooks are either inside a PascalCase function (assumed to be a component) or another useSomething function (assumed to be a custom Hook).
    2. Hooks are called in the same order on every render.
  • Question 237

    What is the difference between Imperative and Declarative in React?

    Imagine a simple UI component, such as a "Like" button. When you tap it, it turns blue if it was previously grey, and grey if it was previously blue.

    The imperative way of doing this would be:

    if (user.likes()) {
    if (hasBlue()) {
    removeBlue();
    addGrey();
    } else {
    removeGrey();
    addBlue();
    }
    }
    

    Basically, you have to check what is currently on the screen and handle all the changes necessary to redraw it with the current state, including undoing the changes from the previous state. You can imagine how complex this could be in a real-world scenario.

    In contrast, the declarative approach would be:

    if (this.state.liked) {
    return <blueLike />;
    } else {
    return <greyLike />;
    }
    

    Because the declarative approach separates concerns, this part of it only needs to handle how the UI should look in a specific state, and is therefore much simpler to understand.

  • Question 238

    What are the benefits of using TypeScript with ReactJS?

    Below are some of the benefits of using TypeScript with ReactJS,

    1. It is possible to use latest JavaScript features
    2. Use of interfaces for complex type definitions
    3. IDEs such as VS Code was made for TypeScript
    4. Avoid bugs with the ease of readability and Validation
  • Question 239

    How do you make sure that user remains authenticated on page refresh while using Context API State Management?

    When a user logs in and reload, to persist the state generally we add the load user action in the useEffect hooks in the main App.js. While using Redux, loadUser action can be easily accessed.

    App.js

    import { loadUser } from "../actions/auth";
    store.dispatch(loadUser());
    
    • But while using Context API, to access context in App.js, wrap the AuthState in index.js so that App.js can access the auth context. Now whenever the page reloads, no matter what route you are on, the user will be authenticated as loadUser action will be triggered on each re-render.

    index.js

    import React from "react";
    import ReactDOM from "react-dom";
    import App from "./App";
    import AuthState from "./context/auth/AuthState";
    
    ReactDOM.render(
    <React.StrictMode>
    <AuthState>
    <App />
    </AuthState>
    </React.StrictMode>,
    document.getElementById("root")
    );
    

    App.js

    const authContext = useContext(AuthContext);
    
    const { loadUser } = authContext;
    
    useEffect(() => {
    loadUser();
    }, []);
    

    loadUser

    const loadUser = async () => {
    const token = sessionStorage.getItem("token");
    
    if (!token) {
    dispatch({
    type: ERROR,
    });
    }
    setAuthToken(token);
    
    try {
    const res = await axios("/api/auth");
    
    dispatch({
    type: USER_LOADED,
    payload: res.data.data,
    });
    } catch (err) {
    console.error(err);
    }
    };
    
  • Question 240

    What are the benefits of new JSX transform?

    There are three major benefits of new JSX transform,

    1. It is possible to use JSX without importing React packages
    2. The compiled output might improve the bundle size in a small amount
    3. The future improvements provides the flexibility to reduce the number of concepts to learn React.
Get LinkedIn Premium at Rs 399