InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What are Pure Functions and how they are used in reducers? |
|
Answer» A Pure function is a function which : Consider the below example: Math.random(); // => 0.4011148700956255 Math.random(); // => 0.8533405303023756 Math.random(); // => 0.3550692005082965Even though we didn’t pass any arguments into any of the function CALLS, they all produced different output, meaning that `Math.random()` is not pure. Now consider the below add function doesn’t alter “a” or “b”, always returns the same output for the same input. The SECOND condition is that it should not produce any side effects. It basically means it should not change any external state. Consider, the below CODE. The function addNum changes the values of external “a”, so it’s not a pure function. var a = 10; function addNum(num) { a = 20; return num + a; } console.log(addNum(5)); //25 console.log(a); //20Reducers are Pure Functions as they don’t mutate the state. It generates a new state which is a copy of the old state. const initialState = { indPopData: [], }; const dataReducer = (state = initialState, action) => { LET newState; switch (action.type) { case types.GET_INDIA_DATA: newState = { ...state, indPopData: action.indPopData }; break; default: newState = state; } return newState; }; |
|
| 2. |
What is the difference between this.state.name=”Thomas” and this.setState({name: “Thomas”}) ? |
|
Answer» FIRST of all setting the STATE directly by this.state.name=”Thomas” is not recommended in REACT. One more drawback of setting state directly is that React’s lifecycle methods — shouldComponentUpdate(), componentWillUpdate(), componentDidUpdate() and render()- depend on state transitions being CALLED with setState(). |
|
| 3. |
Explain type-checking with PropTypes in React? |
|
Answer» JavaScript is a loosely-bound language, which causes many errors in code. So, we should implement type-check, for different props been passed from Parent to Child component. Earlier it used to be a part of React library but since React v15.5, it has moved as a separate library. So, we need to npm install it and then import it by- import PropTypes from ‘prop-types’;Let consider the below example for PropType. import React from ‘react’; class ReactProptypesDemo extends React.Component { render() { return ( <div> <TestForProptypes str="HRIDAY" strOrNumber={10} arr={[5,6,2]} arrOfObject={[{name: "Chinu", age: 36}, {name: "Shik", age: 35}]} /> </div> ) } } CONST TestForProptypes = (props) => { return ( <React.Fragment> <h3>{props.str}</h3> <H4>{props.strOrNumber}</h4> <div>{props.arr.map(ITM=> <li key={itm}>{itm}</li>)}</div> <div>{props.arrOfObject.map(itm=> <li key={itm.age}>{itm.name}</li>)}</div> </React.Fragment> ); } TestForProptypes.propTypes = { str: PropTypes.string, strOrNumber: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), arr: PropTypes.arrayOf(PropTypes.number), arrOfObject: PropTypes.arrayOf(PropTypes.shape( { name: PropTypes.string, age: PropTypes.number } )) } ReactDOM.render(<ReactProptypesDemo />, document.querySelector("#app"))Here we have a parent component ReactProptypesDemo which is passing props to TestForProptypes. So, for each prop, we check whether it is the correct type or not. In TestForProptypes.propTypes , we check for each prop. Like for the “str” prop, we can check by - str: PropTypes.stringFor the array prop “arr” we can check as - arr: PropTypes.arrayOf(PropTypes.number)For the array of object prop “arrOfObject” we can check as - arrOfObject: PropTypes.arrayOf(PropTypes.shape( { name: PropTypes.string, age: PropTypes.number } ))We have a check ALSO for String or Number prop “strOrNumber” as below - strOrNumber: PropTypes.oneOfType([PropTypes.string, PropTypes.number])if we pass “boolean” to it instead of string or number. <TestForProptypes str="Nabendu" strOrNumber={true} arr={[3,1,2]} arrOfObject={[{name: "Nabs", age: 36}, {name: "Shikha", age: 35}]} />We will get the below error in console. Warning: Failed prop type: Invalid prop `strOrNumber` supplied to `TestForProptypes`. in TestForProptypes (created by ReactProptypesDemo) in ReactProptypesDemo |
|
| 4. |
Explain Fragments in React 16? |
|
Answer» React Fragments is a much-needed feature introduced in React 16.2 that helps US to get rid of wrapper div, used inside components. We will consider the below React code. Here we have a code for a simple TODO, which has an adjacent “h2” and a “ol” TAG. render() { return ( <h2>Todos:</h2> <ol> {this.state.items.map(item => ( <li key={item.id}> <label> <input type="checkbox" checked={item.done} /> <span className={item.done ? "done" : ""}>{item.text}</span> </label> </li> ))} </ol> ) } }If we run the above code like this only, we will get the most common React error. SyntaxError: Inline Babel script: Adjacent JSX elements must be wrapped in an enclosing tag (21:8) 19 | return ( 20 | <h2>Todos:</h2> > 21 | <ol> | ^ 22 | {this.state.items.map(item => ( 23 | <li key={item.id}> 24 | <label> To FIX this error, we will wrap everything inside a <div> tag. render() { return ( <div> <h2>Todos:</h2> <ol> {this.state.items.map(item => ( <li key={item.id}> <label> <input type="checkbox" checked={item.done} /> <span className={item.done ? "done" : ""}>{item.text}</span> </label> </li> ))} </ol> </div> ) }Now, the unnecessary “div” creates a lot of problems. Suppose, we are using Flexbox in our parent component and this was a Child Component and the <h2> and <ol> were flex children. Now we introduce this <div> since in Flexbox it will be considered as a flex item, this will break our CSS style. Now, we can use Fragments instead of <div> to get rid of the additional problem it caused. Fragment doesn’t have any special meaning to browser, so the problem of Flexbox item is solved. render() { return ( <React.Fragment> <h2>Todos:</h2> <ol> {this.state.items.map(item => ( <li key={item.id}> <label> <input type="checkbox" checked={item.done} /> <span className={item.done ? "done" : ""}>{item.text}</span> </label> </li> ))} </ol> </React.Fragment> ) }We can also get rid of React.Fragment, if we import Fragment in the import statement. import React, { Component, Fragment } from 'react'; ... render() { return ( <Fragment> <h2>Todos:</h2> <ol> {this.state.items.map(item => ( <li key={item.id}> <label> <input type="checkbox" checked={item.done} /> <span className={item.done ? "done" : ""}>{item.text}</span> </label> </li> ))} </ol> </Fragment> ) } |
|
| 5. |
Explain the use of Webpack and Babel in ReactJS? |
|
Answer» Webpack and Babel are the tools which run in the background to turn our React project into vanilla HTML, CSS, and JavaScript. It is necessary because that only can be PROCESSED by the browser. Webpack in BASICALLY a module builder. Webpack mainly runs during the development process. We have TAKEN the below diagram from https://webpack.js.org/ website. It is quite self-explanatory on what actually webpack does. As per the diagram, it takes your “js”, “sass” and other files and converts them to vanilla “js”, “css”, “jpg” and “png” files, which the web browser can understand. Now, when we create our app with create-react-app, webpack is included in it by the FACEBOOK team. If we are using something like SCSS in our project, it gets converted into vanilla CSS. Babel, on the other hand, is a JavaScript compiler that converts latest JavaScript like ES6, ES7 into plain old ES5 JavaScript that most browsers understand (even the old ones- like IE8).
A simple example of what Babel does for an arrow function, which we introduced in ES6. // Babel Input: ES6 arrow function [5, 9, 7].map((i) => i + 1); // Babel Output: ES5 [5, 9, 7].map(function(i) { return i + 1; });Below is how a simple React JSX line gets converted into the way it is actually used inside React with createElement. |
|
| 6. |
Explain “refs” in React? |
|
Answer» Refs are generally avoided in React and should be used with precautions. It is because they DIRECTLY interact with the Real DOM and in React that is not desirable behaviour. We can, however, use refs in the following situations.
There are many ways to use “refs” but we will see the way introduced in React 16.3 and that is by using React.createRef() In the example for Ref, we have an input box. When we CLICK on the submit button, we’ll read the value ENTERED by the user and log it to the console. The same thing we could have achieved by using normal state and setState also. class RefTextInputDemo extends React.Component { constructor(props) { SUPER(props); // create a ref to store the text Input element this.inputNewItem = React.createRef(); } handleClick = e => { e.preventDefault(); console.log(this.inputNewItem.current.value); }; render() { // tell React that we will associate the <input> ref // with the `inputNewItem` that was created in the constructor return ( <div> <form onSubmit={e => this.handleClick(e)}> <input type="text" ref={this.inputNewItem} /> <button>Submit</button> </form> </div> ); } } |
|
| 7. |
How to use a library like jQuery in React, which interacts directly with DOM? |
|
Answer» Using jQuery in REACT is not recommended as it directly interacts with the DOM and is generally a bad practice. We should always try first to find a React equivalent of the jQuery plugin or WRITE the plugin ourselves. But many times there are plugins, which are only available in jQuery and no other alternative in ReactJS and also very time consuming to write ourselves. In such cases, we can use the method described below. We attach a “ref” element to the root DOM element. Inside componentDidMount, we will GET a reference to it, so that we can pass it to the jQuery plugin. To prevent React from interacting with the DOM after mounting, we will return an empty <div /> from our render() method. The <div /> element has NOTHING, so React will not update it, leaving the jQuery plugin to manage only that PART of the DOM. Also, we will use componetWillUnmount() to unmount the jQuery plugin, after its work with DOM is done. class anotherJqueryPlugin extends React.Component { componentDidMount() { this.$el = $(this.el); this.$el.anotherJqueryPlugin(); } componentWillUnmount() { this.$el.anotherJqueryPlugin('destroy'); } render() { return <div ref={el => this.el = el} />; } } |
|
| 8. |
Explain Virtual DOM in React? |
|
Answer» Sites built with VANILLA JavaScript directly updates the Real DOM. It is fine for smaller websites, but as your website grows complex, it slows down the website. This happens because the browser engine has to traverse all nodes even if you update only one node. Even Angular updates or work on Real DOM. ReactJS doesn’t update the Real DOM directly but has a concept of Virtual DOM. This causes a great performance benefit for ReactJS and so it’s faster than vanilla JavaScript apps and also Angular apps.
After that, it updates the Real DOM, whenever it’s best to update it. |
|
| 9. |
What is state in React? |
|
Answer» When we want to change the data inside a Component we use state. It is one of the most important and difficult concepts in React to understand. Initializing State In our render method, we are CHECKING the value of spinLogo by this.state.spinLogo. It is initially true, so the logo will be displayed. Now, we have a button which has an EVENT listener onClick. When we click the button it runs the function updateLogo() and we change the state. Changing the state To modify the state, simply call this.setState(), passing in the NEW state object as the argument. We’ll do this inside a method which we’ll call updateLogo. So, here we change the value of updateLogo to the opposite of it. Initially, it was true, so we make it FALSE. One important concept in React is that whenever we call this.setState(), it re-renders the Component. So, this time when the ternary operator checks the value of this.state.spinLogo, it is false. So, it displays the link. Click the button again and it will show the logo. |
|
| 10. |
What are props in React? |
|
Answer» Props or properties are REACT way to pass data from parent component to child component. Props can be passed to both Function based component or CLASS-based components. Consider this React CODE for About Page. import React from ‘react’; import Header from ‘./HeaderPage’; import Footer from ‘./FooterPage’; const AboutPage =() => { return( <div> <Header text=“Hero Company” subText=“Cool Company” /> <h1>About Us</h1> <p>We are a cool web development company, located in SILICON Valley.</p> <Footer /> </div> ) }; export default AboutPage;Now the code for Header. import React from ‘react’; const Header =() => { return( <div> <h1>{props.text}</h1> <h2>{props.subText}</h2> </div> ) };export default Header; In the above, we pass two props text and subText from Parent Component AboutPage to Child Component Header. We access the props in Child Component by the keyword props dot and the PROP name. Notice, that we used the {} because props.text is a JavaScript statement and that is how we access JavaScript inside React. There is a slightly different way to access props in class-based components. Here we access props in child components by this.props. Changing our Header Component to class-based component. Consider this React code for About Page. import React from ‘react’; import Header from ‘./HeaderPage’; import Footer from ‘./FooterPage’; const AboutPage =() => { return( <div> <Header text=“Hero Company” subText=“Cool Company” /> <h1>About Us</h1> <p>We are a cool web development company, located in Silicon Valley.</p> <Footer /> </div> ) }; export default AboutPage;Now the code for Header. import React, { Component } from ‘react’; class Header extends Component { render() { return( <div> <h1>{this.props.text}</h1> <h2>{this.props.subText}</h2> </div> ) } export default Header; |
|
| 11. |
What are Components in React? |
|
Answer» EVERYTHING in React are referred as Components. We can have ONE giant component containing all our code, but React’s best practices are to divide our logic into smaller components and reuse them. Like we have a header component, footer component, Service Page Component, Home Page component and so on. There are actually two ways to create Components - Functional Components and Class Based components. We have already seen examples of both types of Components. Below is an EXAMPLE of the functional component. As the name suggests, it is created using functions. import React from ‘react’; const DemoPage =() => { return( <div> <h1>{1+1}</h1> </div> ) }; export default DemoPage; Now we have seen Class-Based components also earlier. import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; class App extends Component { render() { var i =1; return ( <div className="App"> <header className="App-header"> { i === 1 ? <img src={logo} className="App-logo" alt="logo" /> : <a href="https://reactjs.org" target="_blank"> Learn React </a> } </header> </div> ); } } export default App;This is a simple class-based component. But most class-based components have the concept of STATE(which we will SEE later), which is not possible in Function based Components. |
|
| 12. |
Explain JSX in React? |
|
Answer» JSX is used in React to write code instead of regular JavaScript. It is similar to JavaScript but actually an extension to it. JSX Allows us to include ‘HTML’ in the same file along with ‘JavaScript’ (HTML+JS=JSX). JSX LOOKS like regular HTML and we have used it in Question 1. import React from ‘react’; import Header from ‘./HeaderPage’; import Footer from ‘./FooterPage’; CONST AboutPage =() => { return( <div> <Header /> <h1>About Us</h1> <p>We are a cool web development company, located in Silicon Valley.</p> <Footer /> </div> ) }; export default AboutPage;In the above example inside the return statement, we are using JSX. Notice, the <div> is used to wrap the whole HTML code. It is required or else compiler will throw an error. JavaScript expressions also can be used inside of JSX. We use it by wrapping them inside a set of curly brackets {}, as in below example. import React from ‘react’; const DemoPage =() => { return( <div> <h1>{1+1}</h1> </div> ) }; export default DemoPage;Gives output - 2 We cannot use if-else statement inside JSX, so we use ternary expressions if we need to check condition. In the below example, we TOOK the App.js provided when we create a react app using create-react-app. Here we MODIFIED the code and used a ternary EXPRESSION to check if i is 1, and then display an image or else display the link. import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; class App extends Component { render() { var i =1; return ( <div className="App"> <header className="App-header"> { i === 1 ? <img src={logo} className="App-logo" alt="logo" /> : <a href="https://reactjs.org" target="_blank"> Learn React </a> } </header> </div> ); } } export default App;Gives output - |
|
| 13. |
What are the main differences between Angular and React? |
|
Answer» Both Angular and React are popular JavaScript Framework/Library used worldwide to create complex web-apps. Angular is different then AngularJS, which was released in 2009 and uses JavaScript. Angular released in 2016 by Google, was a complete rewrite of AngularJS and is not backward compatible. It uses TypeScript instead of JavaScript as the programming language. TypeScript is a statically TYPED language, which is CONSIDERED as the superset of JavaScript. TypeScript provides developers with Object-oriented background a smooth transition to JavaScript. React is a JavaScript library released by Facebook in 2013. It was initially developed at Facebook to optimise the ease of development of Component and to speed up Facebook. Let’s now look at the differences between Angular and React - Angular is a complete framework, whereas React is a library. React is very lightweight and we generally use other third-party libraries to enhance its capabilities when REQUIRED. On the other hand, you get everything inside the Angular ecosystem only, but sometimes its an overkill. Angular uses two-way data-binding, whereas React uses one-way data-binding. In React the data flows from parent component to child component only. Sometimes this complicates thing and to solve it we use state management packages like Redux and Flux with it. Angular comes with complete MVC(Model-View-Controller), whereas React is just the View layer in MVC. In Angular, we write our TypeScript and HTML in different FILES and it separates the logic. Whereas in React, we use a hybrid language called JSX, which is basically writing JavaScript inside HTML code. React is faster than Angular because it updates the Virtual DOM, whereas Angular updates directly the Real DOM. React only updates the part of DOM which gets updated by the code and behind the scene updates the Real DOM, when its efficient, Angular directly updates Real DOM, like a typical vanilla HTML, JS application. This has a huge impact in complex web-applications which have many complex features and it gives the advantage to React. React is much easier to learn for a beginner as it’s a library with some basic concept. In comparison, Angular learning curve is more as you have to learn a complete Framework and have many unique concepts. But once you need to implement things like Redux, react-router to React, the complexity grows. |
|
| 14. |
What is MVC and where does React stand in it? |
|
Answer» MVC or Model-VIEW-Controller is a software design pattern mainly used in Web development for creating complex web-apps. This architecture comes from the traditional flow of web-application:
Now, React is considered as the View LAYER in the MVC model, as it is mainly used to create the UI. |
|
| 15. |
How to start a project in React? |
|
Answer» There are mainly TWO ways to start a project in React - First is to setup React with Webpack and Babel from Scratch. The second way is to use the user-friendly way provide by Facebook ie using create-react-app. The create-react-app also under the hood uses babel and webpack to compile your React code to PLAIN HTML, CSS, and JavaScript. So, we can set them manually in React. But it is a lengthy and complicated process. On the other hand, create-react-app is a simple way to start with React and provided by Facebook. The way is a bit different for npm version 5.2 and up then for 5.1 and down. We will look into the way to create a react app using rpm version 5.2+. So, first, go to your terminal to check your npm version. As I have npm version 6.5, so will use npx create-react-app my-app to create the react app. It will show US the above message once the SYSTEM is ready. Next we cd into the directory and run our local development environment by running npm start If it compiled successfully, we will get the below in our terminal. As instructed, we need to open any web browser and go to http://localhost:3000/ to see our React app. If you use npm 5.1 or EARLIER, you can't use npx. Instead, install create-react-app globally: npm install -g create-react-app Now you can run: create-react-app my-app |
|
| 16. |
What is ReactJS? |
|
Answer» ReactJS or simply React is a frontend library created by Facebook. It is used in their products like Facebook, WHATSAPP, and Instagram. It was initially maintained by Facebook but later it was MADE open source and now have an active developer community. Popular websites like Netflix, Airbnb, Yahoo! Mail, KhanAcademy, DROPBOX and much more use React to build their UI. React allow us to create reusable UI components. Suppose you have a website, which has the same header and footer in all pages. If you create it using normal HTML and JS, then you have to use the same HTML code for header and footer in all pages. But in React you can have the Header and Footer as a separate component and import it into different Pages. Now, Consider this React code for Services page. import React from ‘react’; import Header from ‘./HeaderPage’; import Footer from ‘./FooterPage’; const ServicePage =() => { return( <div> <Header /> <h1>Services</h1> <p>We provide service in Web development, MOBILE development, SEO services.</p> <Footer /> </div> ) }; export default ServicePage;Beside this React is also very fast which reduces the loading TIME of even complex web-apps. React achieves this speed because it doesn’t update the Real DOM directly but it updates the Virtual DOM. |
|
| 17. |
Explain the new feature of React hooks introduced in React v16.8? |
|
Answer» As per the official React website “Hooks let you use state and other React features without writing a class.” When Class BASED components are small then the state logic is manageable, but as the code grows the state logic becomes unmanageable. In that when we add the lifecycle components like - componentDidMount and componentDidUpdate, the logic becomes more complicated. With hooks, more of React features can be used without the need for classes in a Function-based component. Let’s first look into the example with class-based component. import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; const divStyle = { display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', BORDER: '5px solid pink' }; class App extends Component { constructor() { super(); this.state = { count: 0 } this.setCount = this.setCount.bind(this); } setCount() { this.setState({ count: this.state.count + 1 }) } render() { RETURN ( <div style={divStyle}> <img SRC={logo} className="App-logo" alt="logo" /> <p>You clicked {this.state.count} times</p> <button onClick={this.setCount}>Click me</button> </div> ) } } export default App;It will output a simple button, which on click will increase the count. Now let's change the code to use hook. import React, { useState } from 'react'; import logo from './logo.svg'; import './App.css'; const divStyle = { display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', border: '5px solid pink' }; const App = () => { // Declare a new state variable, which we'll call "count" const [count, setCount] = useState(0) return ( <div style={divStyle}> <img src={logo} className="App-logo" alt="logo" /> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ) } export default App;The keyword useState is our Hook. This code very much cleaner and understandable than that of the Class based component. We call it inside a functional component to add a state to it. React will preserve this state for us. useState always returns a pair which has the current state value and a function that will LETS us to update the state value. We then call this function from an event handler onClick. It’s similar to this.setState in a React class. The argument in useState is used to set the initial state. In our example given above, the initial state is 0, as our counter starts from zero. |
|
| 18. |
What is the use of middleware Redux thunk? |
|
Answer» Redux thunk is a middleware which sits between action creator and reducer in the React-Redux flow. It is very USEFUL when we do asynchronous API calls with FETCH or axios, which returns a Promise and we then dispatch it to reducer. Redux thunk mainly works behind the scene. We have used some boiler-plate to use it. //App.js IMPORT React from 'react'; import ReactDOM from 'react-dom'; import Routes from './routes'; import STORE from './store'; import { Provider } from 'react-redux'; ReactDOM.render( <Provider store={ store }> <Routes /> </Provider>, document.getElementById('root') ); //store.js import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from '../reducer'; const store = createStore(rootReducer, applyMiddleware(thunk)); export default store;In Question 22 for React-Redux flow, in action creator, we call an API endpoint with axios. It is a network request and will take some MILLISECONDS or more depending on the connection. Now, thunk middleware sort of wait for that call, which is a Promise to completed. Once we get the response, which contains the data then only it dispatches it to the reducer. export const getIndPopData = (currYear) => dispatch => { return axios.get(`http://api.population.io:80/1.0/population/${currYear}/India/`).then(response => { dispatch({ type: GET_INDIA_DATA, indPopData: response.data }); }); };So, Redux thunk is required in Projects where we do API calls to some endpoint. |
|
| 19. |
Explain the flow in a React-Redux app? |
|
Answer» As on Redux site “Redux is a predictable state container for JavaScript apps.” Redux can be used separately, but it gained much popularity because it was able to solve the state problem in a REACT app. In React to pass DATA(or state) between COMPONENT, we use props to be passed from Parent to Children. If the data needs to be passed to Components 5 level deep then it has to just have to pass through 4 Components, which doesn’t require it. Passing data from Child to Parent is also a problem, and we need to use the Callback function. This gets complicated soon in a large application. So, to solve this issue we maintain state, which is the main data of the application in a CENTRAL location. It can be accessed by any Components which ask for it. Let see the complete flow. The Container is a file that corresponds directly to a single component. It have two functions called ‘mapDispatchToProps’ and ‘mapStateToProps’. The Action Creator: In this file, you will write the functions that dispatch an action. It performs some action, like an API call using axios. When we get the response back from it, we will dispatch an Object with “type” and “data” as { type: GET_INDIA_DATA, indPopData: response.data } The Reducer hears: an action, and can now generate a new state BASED on what the action wants it to do. Note the state never actually changes in Redux, but instead, the reducer generates a new state which is a copy of the old state. In the code below we don’t mutate the state but create a new state by Object destructuring. const initialState = { indPopData: [], }; const dataReducer = (state = initialState, action) => { let newState; switch (action.type) { case types.GET_INDIA_DATA: newState = { ...state, indPopData: action.indPopData }; break; default: newState = state; } return newState; };Back to Container: the result is received by “mapStateToProps”. It can be accessed as a prop ie “this.props.indPopData” in this case. Here, we are also data massaging the data in componentWillReceiveProps and storing it in local state variables “indPieData” and “indPopTotal” After that it is rendered in the component using “this.state.indPopTotal” and “this.state.indPieData” ... componentWillReceiveProps(nextProps) { if (this.props.indPopData !== nextProps.indPopData) { this.setState({ indPieData: nextProps.indPopData.map(item => {return {name: item.age, value:item.total}})), indPopTotal: nextProps.indPopData.map(item => {return {name: item.age, value:item.total}}); } } ... render() { return ( <Fragment> {this.state.indPopTotal && <p style={totalText}> India - {this.state.indPopTotal} </p>} {this.state.indPieData && <PopPieChart popPieData={this.state.indPieData} />} </Fragment> ) } const mapStateToProps = ({ dataReducer }) => ({ indPopData: dataReducer.indPopData, ... }); |
|
| 20. |
Explain the new feature of Lazy loading and code splitting in React v16.6? |
|
Answer» Lazy loading is the new feature introduced in React v16.6, which allows for some COMPONENTS to load later than other components. This way we can load the components which are fast like text earlier and components which LOADS images a bit later.
Now, we are lazy loading myComp as it has an image to load. Note the special way to import it and also we need to wrap the component in Suspense. Now, Suspense will contain the FALLBACK Component which will be shown while myComp gets loaded. The other component ContentComponent will load instantly. //App.js import React, { Component, lazy, Suspense } from "react"; import "./App.css"; import ContentComponent from './components/ContentComponent'; const MyComp = lazy(() => import("./components/myComp")); class App extends Component { render() { return ( <div className="App"> <header className="App-header"> <h1>Lazy Loading Demo</h1> <Suspense fallback={<div>Loading.....</div>}> <MyComp /> </Suspense> <ContentComponent /> </header> </div> ); } } export default App; //ContentComponent.js import React from 'react' export default function ContentComponent() { return ( <div> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry...</p> <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout...</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...</p> </div> ) } //myComp.js import React from "react"; export default () => { return <img src="https://images.unsplash.com/photo-1517694712202-14dd9538aa97" width="960" height="480" alt="coding" />; };React is very fast and in localhost, condition to see it, we have to emulate Slow speed. To do so open the CONSOLE, and go to Network tab. Then click on Online and select Slow 3G. Now, when you refresh the local running app, you can see Loading…..coming. |
|
| 21. |
Explain the new feature of React Memo in React v16.6? |
|
Answer» The new feature of React Memo are used to solve the problem which we get if the state is not updating in this.setState, but STILL all components are re-rendered. To solve this problem we use two solutions. One is to CHECK and COMPARE state in shouldComponentUpdate and if they are same, we don’t re-render components. The other solution is to use PureComponents. See question 51for details. But both of them can’t be used with functional components. Let’s look at the problem again. Here we have a FunctionalComp, which gets the same state passed every 3 sec. import React, { Component } from "react"; import FunctionalComp from "./components/functionalComp"; import "./App.css"; class App extends Component { state = { val: 1 }; componentDidMount() { setInterval(() => { this.setState({ val: 1 }); }, 3000); } render() { RETURN ( <div className="App"> <header className="App-header"> <FunctionalComp val={this.state.val} /> </header> </div> ); } } export default App;The FunctionalComp is below. import React from “react"; export default (props) => { console.log("val =", props.val); return <div>{props.val}</div>; };So, if we run it we get a value of 1 every 3 seconds printed like below. We can solve it by wrapping the component in React.memo. import React from “react"; export default React.memo(props => { console.log("val =", props.val); return <div>{props.val}</div>; });Now the output is just one 1 and after that, as every time the state of it is set to one, so no more RENDERS. |
|
| 22. |
How to pass data between sibling components using React router? |
|
Answer» We can pass data between React sibling components using React Router using history.push and match.params. Let look into the code. We have a Parent component APP.js. We have two CHILD Components HomePage and AboutPage. Everything is inside a Router from React-router Route. We also have a route for /about/{params}. This is where we will pass the data. import React, { Component } from ‘react’; class App extends Component { render() { return ( <Router> <div className="App"> <ul> <li> <NavLink to="/" activeStyle={{ color:'green' }}>Home</NavLink> </li> <li> <NavLink to="/about" activeStyle={{ color:'green' }}>About </NavLink> </li> </ul> <Route PATH="/about/:aboutId" component={AboutPage} /> <Route path="/about" component={AboutPage} /> <Route path="/" component={HomePage} /> </div> </Router> ); } } EXPORT default App;The HomePage is a simple FUNCTIONAL component, which have a button. On clicking the button we are using props.history.push(‘/about/’ + data) , which is used to programatically navigate to /about/data export default function HomePage(props) { const handleClick = (data) => { props.history.push('/about/' + data); } return ( <div> <button onClick={() => handleClick('Nabendu')}>To About</button> </div> ) }The AboutPage is also a simple functional component, which gets the passed data by props.match.params.aboutId export default function AboutPage(props) { if(!props.match.params.aboutId) { return <div>No Data Yet</div> } return ( <div> {`Data from HomePage ${props.match.params.aboutId}`} </div> ) }The Page after clicking on the button in the HomePage looks like below. |
|
| 23. |
What are Higher Order Components in React? |
|
Answer» In vanilla JavaScript, we have the concept of Higher Order Functions, in which we pass a function to other function as an ARGUMENT. In React Land higher-order Components are very similar to that, and in it, we pass a Component as an argument to other Component, which can also add some functionalities to it. Let’s first CONSIDER the below example without Higher-Order Components. Here we are having a ButtonStyled component, which has two of stylee. If we pass the props “disable”, then the background-color and color are different. You can find the JSFiddle below. const styles = { default : { backgroundColor: '#737373', color: '#eae8e8', padding: '10px' }, disable : { backgroundColor: '#9c9c9c', color: '#c7c6c6', } } class WithoutHOCdemo extends React.Component { render() { RETURN ( <div> <ButtonStyled /> <ButtonStyled disable /> </div> ) } } const ButtonStyled = (props) => { let _styles = {...styles.default}; if(props.disable) _styles = {..._styles, ...styles.disable} return <button style={_styles}>Button Styled</button> } ReactDOM.render(<WithoutHOCdemo />, document.querySelector("#app"))We will now be changing it to use Higher Order Components- styledWrapper, we will move all LOGIC of style changes from ButtonStyled component to a HOC. So, now our ButtonStyled component LOOKS like below. At the last line, we export the styledWrapper wrapping our current component. import React from ‘react’; import styledWrapper from ‘./../HOC/styledWrapper'; const ButtonStyled = (props) => { return (<button style={props.styles}>ButtonStyled</button>) }export default styledWrapper(ButtonStyled); The Higher Order Component styledWrapper will be like below. In the last part, we take the WrapComponent which is ButtonStyled and return it with translatedProps, which will also get the props passed. import React from ‘react’; const styles = { default : { backgroundColor: '#737373', color: '#eae8e8', padding: '10px' }, disable : { backgroundColor: '#9c9c9c', color: '#c7c6c6', } } const translatedProps = (props) => { let _styles = {...styles.default} if(props.disable){ _styles = {..._styles, ...styles.disable}; } const newProps = {...props, styles:_styles } return newProps; } export default (WrapComponent) => { return function wrappedRender(args) { return WrapComponent(translatedProps(args)); } } |
|
| 24. |
What are Pure Components in React? |
|
Answer» Pure Component are special type of Components in React, which doesn’t re-render itself if the state is not changed. In ReactJS if we do a this.setState, it re-renders the Component and it’s child components. Consider the below example. The initial state VALUE is 1. It has a “componentDidMount” by which we are simulating AJAX call by running a setInterval, every 2 seconds. It has a this.setState, but sets the value again to 1. But because this.setState runs, it re-render the component. import React from ‘react’; class PureDemo extends React.Component { constructor(props) { super(props) this.state={ value: 1 } } componentDidMount() { setInterval(() => { this.setState({ value: 1}) }, 2000) } render() { console.log('Render PureDemo'); return ( <Demo value={this.state.value} /> ) } } const Demo = (props) => { console.log('Render Demo'); return <div>{props.val}</div> } ReactDOM.render(<PureDemo />, document.querySelector("#app"))If we run the below, both components will be rendered after every 2 seconds and we will get the below printed in the console. Now, we can solve this ISSUE of not rendering components if state is not changed, by using lifecycle hook shouldComponentUpdate. It will run in each render and here we are CHECKING if the CURRENT state is equal to the next state. If they are equal, we are returning FALSE and the components will not re-render. class PureDemo extends React.Component { constructor(props) { super(props) this.state={ value: 1 } } componentDidMount() { setInterval(() => { this.setState({ value: 1}) }, 2000) } shouldComponentUpdate(nextProp, nextState) { return (this.state.value === nextState.value ? false : true ) } render() { console.log('Render PureDemo'); return ( <Demo value={this.state.value} /> ) } } const Demo = (props) => { console.log('Render Demo'); return <div>{props.value}</div> } ReactDOM.render(<PureDemo />, document.querySelector("#app"))The same thing can be achieved by using Pure Components, instead of a normal component. It will behave similarly to the above code with shouldComponentUpdate. class PureDemo extends React.PureComponent { constructor(props) { super(props) this.state={ value: 1 } } componentDidMount() { setInterval(() => { this.setState({ value: 1}) }, 2000) } render() { console.log('Render PureDemo'); return ( <Demo val={this.state.value} /> ) } } const Demo = (props) => { console.log('Render Demo'); return <div>{props.value}</div> } ReactDOM.render(<PureDemo />, document.querySelector(“#app")) |
|
| 25. |
Explain new Context API in React 16? |
|
Answer» In React LAND, data is passed top-down from parent component to child component via PROPS. But many times props are needed by a component, which is more than 10 levels deep. In such a scenario, the middle components are just passing the props. This unnecessary passing of props is an issue and is called prop drilling. This is where state management like Redux and Flux comes into the picture, which maintains one single central state. Let’s FIRST look into the example by passing props through an unnecessary component. We have a GRANDFATHER component, which has a state called familyName. This is needed by the Child component. But we also have Father component in between, so we just pass familyName through it. class GrandFather extends React.Component { state = { familyName: "Das" } render() { return <Father familyName={this.state.familyName} /> } } const Father = ({ familyName}) => { return <Child familyName={familyName} /> } const Child = ({ familyName }) => { return <p>{familyName}</p> }We can refactor the above to use the new Context API. Using Context means we don’t need to pass the familyName unnecessary through the <Father /> component. Here first we create our FamilyContext by React.createContext() const FamilyContext = React.createContext({}); class GrandFather extends React.Component { state = { familyName: "Das" }; render() { return ( <FamilyContext.Provider value={this.state.familyName}> <Father /> </FamilyContext.Provider> ); } } const Father = () => { return <Child />; }; const Child = () => { return <FamilyContext.Consumer>{context => <p>{context}</p>} </FamilyContext.Consumer>; }; ReactDOM.render(<GrandFather />, document.querySelector("#app"))Now, we will wrap the <Father /> component with <FamilyContext.Provider /> as it contains <Child />.Notice that the Provider has a value prop. Pass in whatever state you’d like to share to any Component deep down. To have access to the familyName, we have also wrapped the <p> tag in the <FamilyContext.Consumer /> component so that it has access to the context. |
|
| 26. |
Explain the Error boundaries in React 16.3? |
|
Answer» Error boundaries are React components that catch JavaScript ERRORS anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch(). Updating state from these lifecycles lets you capture an unhandled JavaScript error in the below tree and display a fallback UI. static getDerivedStateFromError() componentDidCatch()
As it is called during the commit phase so side-effects like this.setState are allowed. Below is the example where we DEFINE a component called ErrorBoundary class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { // Update state so the next render will SHOW the fallback UI. return { hasError: true }; } componentDidCatch(error, info) { logComponentStackToMyService(info.componentStack); } render() { if (this.state.hasError) { return <H1>Something went wrong.</h1>; } return this.props.children; } }After this we need to wrap any component with it and try will work as the classic try..catch block. <ErrorBoundary> <ProductCard /> </ErrorBoundary> |
|
| 27. |
Explain the new lifecycle methods in React 16.3? |
|
Answer» There are two new lifecycle methods introduced in React 16.3. Their main TASK is replacing the old error-prone lifecycle methods like componentWillUpdate and componentWillReceiveProps. You can still use these old lifecycle methods in your project but only till React 17 is released. These old lifecycle methods are now called - UNSAFE_componentWillUpdate and UNSAFE_componentWillReceiveProps. static getDerivedStateFromProps(nextProps, prevState) getSnapshotBeforeUpdate(prevProps, prevState) So, the new ORDER of mounting is -
The order of update caused by changes in any props or state is -
|
|
| 28. |
Explain lifecycle of react due to all to call to this.setState |
|
Answer» The COMPONENT in which this.setState is called, is re-rendered including its child components. All of the LIFECYCLE methods was already explained in Question 11 and Question 12. Below is the diagram for the same. |
|
| 29. |
Explain lifecycle of component re-rendering due to re-rendering of parent component? |
|
Answer» There is a type of re-rendering, which happens to all child components when the parent component re-renders. It also happens when you are using a React-redux model and the server call request have been completed and you have just received NEW props in mapStateToProps().
If we return false, it means React will not execute the lifecycle methods - componentWillUpdate() and componentDidUpdate() and also the render() If it is not used in the project, then React internally gives the default value of true to it.
|
|
| 30. |
Explain the initial cycle in React which happens during the first render? |
|
Answer» When a React App loads for the first time, the code is RUN in the mentioned order. All the below-mentioned methods run only one time except “render()”, which can be run many times depending on setState and Parent component bee called.
3) Then the initial “render()” will be called. It will also render all the child components(if any) of this component. Also, note that render is generally called many times. WHENEVER we use setState, the component render is called. 4) Then the function “componentDidMount()” will be called. This function will also be called once during the whole life-cycle. It is a great place to do AJAX call to the server to fetch some data. You can also initialize something that requires interaction with the DOM, like a jQuery library. |
|
| 31. |
Why API calls are to be made from componentDidMount and not in constructor/componentWillMount |
|
Answer» This is recommended to avoid side effects. Render method will get CALLED immediately after the componentWillMount and no way we can make the render method wait until the API has returned. Constructor is a place where we define the variables and not make any API calls. API calls can have side effects and should not be used inside constructor React expects state to be AVAILABLE as render function will be called next after componentWillMount and code can break if any MENTIONED state variable is missing which may occur in case of ajax API calls One more reason is If we are doing server-side rendering of React components componentWillMount will get called on the server-side and again on the CLIENT, resulting in calling fetch two times. Hence, this is not definitely the place where we should integrate our APIs. NOTE: A side effect is any application state change that is observable outside the called function other than its return value. Eg: modifying a GLOBAL variable |
|
| 32. |
What are Pure components in React |
|
Answer» React.PureComponent is one of the most significant ways to optimize React applications that is easy and fast to implement. The usage of React.PureComponent gives a considerable increase in performance because it reduces the number of render operation in the application. By default, a plain React.Component has shouldComponentUpdate set to always return true. A common pitfall when converting from Component to PureComponent is to forget that the children need to re-render too. As with all React - if the parent doesn’t re-render the children won’t either. So if you have a PureComponent with children, those children can only update if the parent’s state or props are shallowly DIFFERENT (causing the parent to re-render). This is how React shallow compares (copied from React source code) Code sample below In the given example when we extend Display component from React.Component the render function will be called each time you click the button. Instead if you extend the Display component from React.PureComponent the render function will be called only once. That means it CALLS render when the prop Is shallowly different import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; import Display from "./Display"; function App() { return ( <div className="App"> <Display chek={10} /> </div> ); } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);import React from "react"; CLASS Display extends React.PureComponent { CONSTRUCTOR(props){ super(props); this.state = { obj1: 1 } } updateMe = () => { this.setState({ obj1: this.props.chek }); }; render(){ console.log("render called"); return ( <> <div>{this.state.obj1}</div> <div> <button type="submit" onClick={this.updateMe}> click here </button> </div> </> ) } } export default Display;import React from "react"; class Display extends React.Component { constructor(props){ super(props); this.state = { obj1: 1 } } updateMe = () => { this.setState({ obj1: this.props.chek }); }; render(){ console.log("render called"); return ( <> <div>{this.state.obj1}</div> <div> <button type="submit" onClick={this.updateMe}> click here </button> </div> </> ) } } export default Display; |
|
| 33. |
Explain lifecycle methods and their order of execution |
|
Answer» React lets you define components as classes or functions. Components defined as classes currently provide more features To define a React component class, you need to extend React.Component. class App extends React.Component { render() { return ( <div className="App"> <h2>Hellow World!</h2> </div> ); } }The only method you must define in a React.Component SUBCLASS is called render() All the other methods described on this page are optional. Each component has several “lifecycle methods” that you can override to run code at particular times in the process . Lifecycle methods can be classified into four.
Mounting Methods are listed in the order of their execution constructor() The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs. TYPICALLY, in React constructors are only used for two purposes:
static getDerivedStateFromProps() getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or NULL to update nothing. This method exists for rare use cases where the state depends on changes in props over time. This method doesn’t have access to the component instance. If you’d like, you can reuse some code between getDerivedStateFromProps() and the other class methods by extracting pure functions of the component props and state outside the class definition. NOTE that this method is fired on every render, regardless of the cause. This is in contrast to UNSAFE_componentWillReceiveProps, which only fires when the parent causes a re-render and not as a result of a local setState. render()
The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it’s invoked, and it does not directly interact with the browser. If you need to interact with the browser, perform your work in componentDidMount() or the other lifecycle methods instead. Keeping render() pure makes components easier to think about. componentDidMount() componentDidMount() is invoked immediately after a component is mounted (inserted into the tree). Initialization that requires DOM nodes should go here. If you need to LOAD data from a remote endpoint, this is a good place to instantiate the network request. You may call setState() immediately in componentDidMount(). It will trigger an EXTRA rendering, but it will happen before the browser updates the screen. This guarantees that even though the render() will be called twice in this case, the user won’t see the intermediate state. Use this pattern with caution because it often causes performance issues. In most cases, you should be able to assign the initial state in the constructor() instead. It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position. Updating An update can be caused by changes to props or state. Methods are listed in the order of their execution static getDerivedStateFromProps(): Mentioned above shouldComponentUpdate() Use shouldComponentUpdate() to let React know if a component’s output is not affected by the current change in state or props. The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior. Note that returning false does not prevent child components from re-rendering when their state changes. Currently, if shouldComponentUpdate() returns false, then UNSAFE_componentWillUpdate(), render(), and componentDidUpdate() will not be invoked. In the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component. render(): mentioned above getSnapshotBeforeUpdate() getSnapshotBeforeUpdate() is invoked right before the most recently rendered output is committed to e.g. the DOM. It enables your component to capture some information from the DOM (e.g. scroll position) before it is potentially changed. Any value returned by this lifecycle will be passed as a parameter to componentDidUpdate(). This use case is not common, but it may occur in UIs like a chat thread that need to handle scroll position in a special way.A snapshot value (or null) should be returned. componentDidUpdate()
Unmounting This method is called when a component is being removed from the DOM: componentWillUnmount() componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount(). You should not call setState() in componentWillUnmount() because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted again. Error Handling These methods are called when there is an error during rendering, in a lifecycle method, or in the constructor of any child component. static getDerivedStateFromError() This lifecycle is invoked after an error has been thrown by a descendant component. It receives the error that was thrown as a parameter and should return a value to update state. getDerivedStateFromError() is called during the “render” phase, so side-effects are not permitted. For those use cases, use componentDidCatch() instead. componentDidCatch() This lifecycle is invoked after an error has been thrown by a descendant component. It receives two parameters:
In the event of an error, you can render a fallback UI with componentDidCatch() by calling setState, but this will be deprecated in a future release. Use static getDerivedStateFromError() to handle fallback rendering instead. |
|
| 34. |
What are keys and its significance in Listing |
|
Answer» Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity The best way to pick a KEY is to use a string that uniquely identifies a list item among its SIBLINGS. Most often you would use IDs from your data as keys Keys used within arrays should be unique among their siblings. However they don’t need to be globally unique. We can use the same keys when we produce two different arrays To understand this you need understand how the VIRTUAL DOM works in React. The virtual DOM (VDOM) is a programming concept where an IDEAL, or “virtual”, representation of a UI is kept in memory and synced with the “real” DOM by a library such as ReactDOM. This process is called reconciliation. Each time the underlying data changes in a React app, a new Virtual DOM representation of the USER interface is created Updating browser DOM is as follows
Look at the example given below import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; class App extends React.Component { render() { const numbers = [1,2,3,4,5]; const listItems = numbers.map((number) => <li> {number} </li> ); return ( <div className="App"> <h2>Hellow World!!</h2> <div>{listItems}</div> </div> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);This will throw a warning in console
|
|
| 35. |
Write a program to pass values to child using context. |
| Answer» IMPORT React, { COMPONENT } from "react"; import Intl, { IntlProvider } from "./Intl"; import ReactDOM from "react-dom"; import "./styles.css"; class App extends Component { state = { language: "en" }; handleClick = () => { this.setState({ language: this.state.language === "en" ? "fr" : "en" }); }; render() { return ( <div className="App"> <IntlProvider language={this.state.language}> <p> <Intl id="hello" /> <Intl id="WORLD" />! </p> <p> <button ONCLICK={this.handleClick}>Toggle Language</button> </p> </IntlProvider> </div> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);import React, { createContext } from "react"; const IntlContext = createContext(NULL); const IntlProvider = ({ children, language }) => { const languages = { en: { hello: "hello", world: "world" }, fr: { hello: "Bonjour", world: "monde" } }; return ( <IntlContext.Provider value={languages[language]}> {children} </IntlContext.Provider> ); }; const Intl = ({ id }) => ( <IntlContext.Consumer>{value => value[id]}</IntlContext.Consumer> ); export { IntlProvider }; export default Intl; | |
| 36. |
What all ways data can be passed between react components |
| Answer» | |
| 37. |
What is known as Fragment in React. |
Answer»
|
|
| 38. |
How react router works |
|
Answer» React router is a commonly USED module for routing in React applications. React router COMPONENT listens to history changes in React applications. It has a URL mapping to components in it. It renders the corresponding component when a MATCH is found. Link component is used to NAVIGATE around in your application. Code sample below import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; import { BrowserRouter as Router, Route, Link } from "react-router-dom"; const App = () => ( <Router> <> <Link to="/">Home</Link> <Link to="/page">Page</Link> <Route exact path="/" component={Home} /> <Route path="/page" component={Page} /> </> </Router> ) const Home = () => <H2>Home</h2>; const Page = () => <h2>Page</h2>; const rootElement = document.getElementById("root"); ReactDOM.render(<App label="Save" />, rootElement); |
|
| 39. |
What is the output of the following code |
|
Answer» import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; function APP() { const check = 0; // 0, "", null, undefined return ( <div className="App"> { check && <span>Hello</span> } </div> ); } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement); This will render 0 to the browser.
We need to make the 0 a Boolean by adding !. Code is given below import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; function App() { const check = 0; // 0, "", null, undefined return ( <div className="App"> { !check && <span>Hello</span> } </div> ); } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement); |
|
| 40. |
Write a small piece of code to render a button. |
|
Answer» import React from "react"; import REACTDOM from "react-dom"; import "./styles.css"; function Button(props) { return ( <button TYPE="submit">{props.LABEL}</button> ); } CONST rootElement = document.getElementById("root"); ReactDOM.render(<Button label="Save" />, rootElement); |
|