Best Online Tutorials

SQL Tutorial

Free SQL Tutorial

SQL Interview Questions

SQL Practice Questions

SQL Tips & Tricks

SQL Errors

SQL Books

Angular Tutorial

Free Angular Tutorial

Angular Interview Questions

Angular Practice Questions

Angular Tips & Tricks

Angular Errors

Angular Books

Node.js Tutorial

Free Node.js Tutorial

Node.js Interview Questions

Node.js Practice Questions

Node.js Tips & Tricks

Node.js Errors

Node.js Books

React.js Tutorial

Free React.js Tutorial

React.js Interview Questions

React.js Practice Questions

React.js Tips & Tricks

React.js Errors

React.js Books

SSIS Tutorial

Free SSIS Tutorial

SSIS Interview Questions

SSIS Practice Questions

SSIS Tips & Tricks

SSIS Errors

SSIS Books

SEO Tutorial

Free SEO Tutorial

SEO Interview Questions

SEO Practice Questions

SEO Tips & Tricks

SEO Errors

SEO Books

Javascript Tutorial

Free Javascript Tutorial

Javascript Interview Questions

Javascript Practice Questions

Javascript Tips & Tricks

Javascript Errors

Javascript Books

MongoDB Tutorial

Free MongoDB Tutorial

MongoDB Interview Questions

MongoDB Practice Questions

MongoDB Tips & Tricks

MongoDB Errors

MongoDB Books

Next.js Tutorial

Free Next.js Tutorial

Next.js Interview Questions

Next.js Practice Questions

Next.js Tips & Tricks

Next.js Errors

Next.js Books

React.js Interview Questions - Part 3 

Prashant Surya  |   Mar 16, 2020  |  Views: 72

Description:

21. What are error boundaries?


Error boundaries are React components that catch JavaScript errors anywhere in their child component tree.

It then log those errors, and display a fallback UI instead of the component tree that crashed.

It catches error during rendering, in lifecycle methods, and in constructors of the whole tree below them.

A class component becomes an error boundary if it defines either (or both) of the lifecycle error handler methods static getDerivedStateFromError() or componentDidCatch(). 

22. What is state in reactjs?

State is a JavaScript object in the instance of the component class which hold the information that influences the output of the render.

State is managed within a component.

State is updated with the help of setState() API of component.

When a state changes, the component responds by re-rendering itself.

State is set inside the constructor using keywords this.state

23. How do you update a component’s state?


using setState()

- It is used to edit or update state properties.

24. What happens when state changes?



When a state is changed by a user action or by any event, react's updating life cycle hooks gets executed.

It then re-renders the updated elements in the DOM.

State shouldn’t be modified directly – the setState( ) should be used

State affects the performance of your app, so shouldn’t be used unnecessarily in application.


25. What are the differences between props and state?


Components receive data from outside with props, whereas they can create and manage their own data with state

Props are used to pass data, whereas state is for managing the data

Data from props is read-only, and cannot be modified by a component that is receiving it from outside

State data can be modified by its own component, but is private (cannot be accessed from outside)

Props can only be passed from parent component to child (unidirectional flow)

Modifying state should happen with the setState ( ) method only


26. Why setState gives the wrong value sometime?


Calls to setState are asynchronous, so this.state may not give the correct updated value.

so rather than using the simple setState, use setState(state,props) which contain the current state and props.

27. What are props in reactjs?


Props are the properties.

Props are the immutable data which is passed to the component from the outside.

Props cannot be changed or updated inside any component.

Props properties are accessed by using this.props.propertyName.

The child components should only pass data from the parent component's state using props.

Props can be used with state also.

28. How to set default props value?


We can set default props also using the defaultProps.

class App extends React.Component {
   render() {
      return (
         <div>
            <h1>{this.props.title}</h1>
            <h2>{this.props.desc}</h2>
         </div>
      );
   }
}
App.defaultProps = {
   title: "Reactjs Tutorial",
   desc:"Complete topics from basic to advanced"
}
ReactDOM.render(<App />, document.getElementById('app'));

29. How to implement Props validation?


To avoid any future error while using the props, we can add validation on them.

React.PropTypes is used to assign the types to the props.

class App extends React.Component {
   render() {
      return (
         <div>
            <p>Array: {this.props.propArray}</p>
            <p>Bool: {this.props.propBool ? "True" : "False"}</p>
            <p>Func: {this.props.propFunc(3)}</p>
            <p>Number: {this.props.propNumber}</p>
            <p>String: {this.props.propString}</p>
            <p>Object: {this.props.propObject.objectName1}</p>
            <p>Object: {this.props.propObject.objectName2}</p>
            <p>Object: {this.props.propObject.objectName3}</p>
         </div>
      );
   }
}
App.propTypes = {
   propArray: React.PropTypes.array.isRequired,
   propBool: React.PropTypes.bool.isRequired,
   propFunc: React.PropTypes.func,
   propNumber: React.PropTypes.number,
   propString: React.PropTypes.string,
   propObject: React.PropTypes.object
}
App.defaultProps = {
   propArray: [1,2,3,4,5],
   propBool: true,
   propFunc: function(e){return e},
   propNumber: 1,
   propString: "String value",
   
   propObject: {
      objectName1:"objectValue1",
      objectName2: "objectValue2",
      objectName3: "objectValue3"
   }
}
ReactDOM.render(<App/>, document.getElementById('app'));

30. What is the Difference between props and state?


Props:
props are variables passed to it by its parent component.

This is data maintained from outside of a component.

Props are read-only (immutable) in the child component that receives them.

However, callback functions can also be passed, which can be executed inside the child to initiate an update.


State:
State are variables, which are directly initialized and managed by the component.

This is data maintained inside a component.

The component itself will update the state using the setState function.


class App extends React.Component {
   constructor(props) {
      super(props);
      this.state = {
         title: "Reactjs Tutorial",
         desc:"Complete topics from basic to advanced"
      }
   }
   render() {
      return (
         <div>
            <Title content = {this.state.title}/>
            <Desc content = {this.state.desc}/>
         </div>
      );
   }
}
class Title extends React.Component {
   render() {
      return (
         <div>
            <h1>{this.props.content}</h1>
         </div>
      );
   }
}
class Desc extends React.Component {
   render() {
      return (
         <div>
            <h2>{this.props.content}</h2>
         </div>
      );
   }
}
ReactDOM.render(<App />, document.getElementById('app'));



Comments:
Content Contributor
Prashant Surya

Prashant Surya

Software developer at Uniserve Data Technologies Pvt. Ltd.  |   Noida, Uttar Pradesh, India

I am a software developer, freelancer and trainer working on technologies likes. MEAN stack, MERN Stack, ASP.NET full stack , SQL, JavaScript, jQuery, HTML, CSS, SEO, responsive UI/UX designing... having 7 years of experience in development in multiple domains like, eCommerce, capital markets, education, real estate...


Want to sell your software ?-

If you want to sell your readymade software to the genuine clients or businessman, list your software with details and demo links.

  • Sell ERP
  • Sell Website Clones
  • Sell App Clones
  • Sell Readymade Portals
  • Sell Readymade Websites
  • Sell Mobile Apps
  • Sell Window Apps

Clients will find it using our advanced search filter and will contact you directly.

No any charge for the product lsiting.

Do you own a Company ?+

Property.sale

Buy or Sell Commercial Property+

Are you a freelancer ?+

Looking for a job ?+

Are you a Service Provider ?+

Are you a blogger ?+

Do you own an IT Institute ?+


Property.sale