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

React Fragment in React Js

React Fragment in React Js

It was first introduced in react version 16.2

Fragments lets you to group a list of children without adding extra nodes ( div ) to the DOM.

It is common practice in react Js that, we need to return multiple elements from a component.

Sample Code:

A) App.js ( with div tag ) - There is a one problem associated with below code, clearly it will add one extra div tag which is unnecessary.

import React from "react";

export default function App() {
  return (
    // Extra div tag is added
    <div>
      <h1>Hello</h1>
      <p>Welcome to Vikas Programming</p>
    </div>
  );
}

B) App.js ( with <React.Fragment> tag ) - This will not add any extra element in DOM, so its solution to above problem.

import React from "react";

export default function App() {
  return (
    // No extra div tag is added
    <React.Fragment>
      <h1>Hello</h1>
      <p>Welcome to Vikas Programming</p>
    </React.Fragment>
  );
}

C) App.js ( with Empty tag  <>  ) - We can use empty tag ( <> ) as alternative to ‘<React.Fragment>’ but important thing to remember here is that empty tag don’t have key attribute so it will be not useful for iteration through a list or a collection (Refer Keyed Fragments below ).

import React from "react";

export default function App() {
  return (
    // Same as React.Fragment, shorter notation syntax
    <>
      <h1>Hello</h1>
      <p>Welcome to Vikas Programming</p>
    </>
  );
}

Keyed Fragments:

<React.Fragment> syntax may have key attribute. This will be useful while iterating through a list or a collection as shown below.

D) App.js

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

export default function App() {
  let users = [
    { id: 1firstName: 'vikas'lastName: 'gore' },
    { id: 2firstName: 'vijay'lastName: 'patil' },
  ];
  
  return users.map((user=> (
    // Without the `key`, React will fire a key warning
    <React.Fragment key={user.id}>
      <h2>{user.firstName}</h2>
      <h2>{user.lastName}</h2>
      <h2>{user.address}</h2>
    </React.Fragment>
  ));
}

Benefits of React Fragment:

1)      a) It takes less memory.

2)      b) As div tag is removed while rendering, execution of code is faster.


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