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

useNavigate and useLocation hooks react-router-dom-v6

useNavigate and useLocation hooks - Navigating programmatically to a route.

In this post, we will learn about two new hooks from react-router-dom version 6 library.

1) useNavigate

- Let’s say on click of a button, If you want to perform redirection to the page, then react router provide us with a hook called as useNavigate.

- useNavigate hook replaced useHistory hook, but it works same as useHistory but in addition it has good feature.

- useNavigate hook is only available in react-router-dom version 6.

- We can redirect to Previous and Next page with the help of useNavigate hooks. 

2) useLocation

- We required two library React 16.8+ and react-router-dom 5.1+ for useLocation hook.

- It is a function that returns the location object that contains information about the current URL.

- New location object is created, whenever the URL changed.

useNavigate-useLocation-hooks-react-router-dom-version-6
useNavigate and useLocation hooks react router dom version 6Js and Xstate

Install React Router Command

npm install react-router-dom --save

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 { RoutesRouteBrowserRouter } from 'react-router-dom';
import StudentDetails from './StudentDetails';
import Student from './Student';

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="Student/:studid" element={<Student />} />
        <Route path="StudentDetails" element={<StudentDetails />} />
      </Routes>
    </BrowserRouter>
  );
}

C) Student.js

import React from 'react';
import { useParamsuseNavigate } from 'react-router-dom';

const Student = () => {
  const params = useParams();
  const navigate = useNavigate();

  const onNavigatePageClick = (e=> {
    e.preventDefault();

    // Navigate Student Details to Student page.
    navigate('/StudentDetails/', {
      state: { studId: params.studidstudFirstName: 'Vikas' },
    });
  };
  return (
    <div id="mainDiv">
      <div id="pageName">Student Js Page </div> <br />
      <div>============================ </div> <br />
      <div id="stud">Student - ID : {params.studid} </div> <br />
      <div>============================ </div> <br />
      <button onClick={onNavigatePageClick}>
        Navigate to Student Details Page
      </button>
    </div>
  );
};

export default Student;

D) StudentDetails.js

import React from 'react';
import { useLocation } from 'react-router-dom';

const StudentDetails = () => {
  const { state } = useLocation();

  return (
    <div id="mainDiv">
      <div id="pageName">Student Detail Js Page </div> <br />
      <div>============================ </div> <br />
      <div>Student Detail - Id: {state.studId} </div> <br />
      <div>Student Detail - First Name: {state.studFirstName} </div> <br />
      <div>============================ </div> <br />
    </div>
  );
};

export default StudentDetails;

E) Style.css

#mainDiv {
  border1px solid black;
  height200px;
  width350px;
  margin10px;
  padding5px;
}

#pageName {
  colororange;
  background-colorgray;
  font-weightbold;
}


Happy Learning!! Happy Coding!!

Comments

Popular posts from this blog

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 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

Create a custom calendar in React Js | Interview Question

Environment Setup and Installation for React Js Application