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 fetch data from an API using fetch() method in React Js

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

Now a day, we can't imagine web application without API so one of the basic requirement of any web application is to communicate with servers through HTTP protocol. This can be easily done using Fetch and Axios

In this post, we are mainly focusing on Fetch. The Fetch API provides a fetch() method defined on the window object. The fetch method has one mandatory argument i.e., URL of the resource to be fetched. This method returns promise object that can be used to retrieve response of the request. 

There are following limitations of Fetch API:

a) Fetch does not allows cancelling request and request timeout.

b) Fetch does not provide a way to intercept HTTP requests.

c) Fetch has no url in request object.

d) No installation is required.

e) Fetch does not have XSRF protection.

f) Fetch uses the body property.

g) Fetch is a two step process when handling JSON data, first - to make actual request. second - to call json() method on the response.

fetch-api-react-js
Fetch API React Js
                

Sample Code:

A) index.js

import React from "react";
import ReactDOM from "react-dom";

import App from "./App";

ReactDOM.render(<App />document.getElementById("root"));

B) App.js

import React from "react";
import "./style.css";
import Users from './Users'

export default function App() {
  return (
    <div>
      <Users />
    </div>
  );
}

C) Users.js

import React, { useStateuseEffect } from 'react';
import './style.css';

function Users() {
  const [userDatasetUserData] = useState([]);

  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/users')
      .then((res=> res.json())
      .then((json=> {
        setUserData(json);
      });
  }, []);

  return (
    <div>
      <h1>User Data</h1>
      <div id="divTable">
        {userData.map((item=> (
          <div id="divTableRow" key={item.id}>
            <div id="divTableColumn">{item.username}</div>
            <div id="divTableColumn">{item.name}</div>
            <div id="divTableColumn">{item.email}</div>
          </div>
        ))}
      </div>
    </div>
  );
}

export default Users;

D) style.css

#divTable {
  displaytable;
  widthauto;
  background-color#eee;
  border1px solid #666666;
  border-spacing5px;
}
#divTableRow {
  displaytable-row;
  widthauto;
  clearboth;
}
#divTableColumn {
  floatleft;
  displaytable-column;
  width200px;
  background-color#ccc;
}


Happy Learning!! Happy Coding!!

Comments

Popular posts from this blog

useNavigate and useLocation hooks react-router-dom-v6

How to implement error boundaries in React Js

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

Localization in React Js

Create a Shopping Item App using React Js and Xstate

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

Routing in React using React-Router Version 6

Create a custom calendar in React Js | Interview Question

Environment Setup and Installation for React Js Application