Understanding and Implementing Schemas in Python

Understanding and Implementing Schemas in Python Introduction In the world of programming, particularly in the context of data management and validation, schemas play a vital role. A schema is essentially a blueprint or a predefined structure that defines the expected format, data types, and constraints for a given data entity. In this blog, we will delve into the concept of schemas in Python, exploring what they are, why they are important, and how you can implement them in your projects. What is a Schema? A schema serves as a contract between different components of a system, ensuring that data is consistent, valid, and well-structured. It defines the rules for how data should be organized, what fields it should contain, and what types of values those fields can hold. In essence, a schema acts as a set of rules that data must adhere to in order to be considered valid. Why Are Schemas Important? Data Validation: Schemas provide a way to validate incoming data. When data is received o

How to implement error boundaries in React Js

Error boundaries in React Js

In this post, we will learn about "Error Boundaries in React Js". First of all, why we required Error Boundaries? This is the first question comes in my mind, as before React 16, we don't have any mechanism to catch error inside components and because of which whole app in broken state on next renders. No ways to handle these errors in React Components, nor it gives any methods to recover from them.

React 16 introduces a new concept to handle such errors by using the error boundaries. So now, if any error found in a part of the UI, it does not break the whole application.

According to official document of React Js, There are following few areas in application, where error boundaries are not suitable solution.

a) Event handlers

b) Asynchronous code (e.g., setTimeout)

c) Server-side rendering

d) Errors are thrown in the error boundary itself 

For complex application which contains multiple components, we can declare multiple error boundaries to recover each part of the entire application.

Error Boundaries Class Component contains following important things:

a) getDerivedStateFromError() - It is static method to render a fallback UI when an error has been thrown,

b) componentDidCatch() - to log error information.

Implementation of error boundaries:

a) Create a class which extends React component and passes the props inside it.

b) Add componentDidCatch() method which allows you to catch error in the components below them in the tree

c) Add render() method, it will display the error message or render new component.

d) Now, we can use it as a regular component. Add the new component in HTML, which you want to include in the error boundary. In this example, we are adding an error boundary around a App Component.

<ErrorBoundary>  

       <App />  

</ErrorBoundary>  

Where to place Error Boundaries:

a) It depends on us. we can use error boundaries on the top level of the app components

b) or wrap it on the individual components.

Sample Code:

A) index.js

import React from 'react';
import ReactDOM from 'react-dom';
import ErrorBoundary from './ErrorBoundary';
import App from './App';

ReactDOM.render(
  <ErrorBoundary>
    <App />
  </ErrorBoundary>,
  document.getElementById('root')
);

B) App.js

import React from "react";

export default function App() {
  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
    </div>
  );
}

C) ErrorBoundary.js

import React from 'react';

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(errorerrorInfo) {
    // You can also log the error to an error reporting service
    logErrorToMyService(errorerrorInfo);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children;
  }
}

export default ErrorBoundary;


Happy Learning!! Happy Coding!!

Comments

Popular posts from this blog

useNavigate and useLocation hooks react-router-dom-v6

Pass data from child component to its parent component in React Js

Create a Shopping Item App using React Js and Xstate

Localization in React Js

How to fetch data from an API using fetch() method in React Js

How to fetch data using Axios Http Get Request in React Js?

Routing in React using React-Router Version 6

Environment Setup and Installation for React Js Application

Create a custom calendar in React Js | Interview Question