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

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

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

In this post, we will learn about how to pass data from child component to its parent component in React js.

We need to do following steps to pass/send data from child component to parent component:

a) In the parent component, create a callback function. This callback function is useful to retrieve the data from the child component.

b) Pass the callback function to the child as a props from the parent component.

c) The child component calls the parent callback function using props and sends the data to the parent component.

How-to-pass-data-from-child-component-to-its-parent-component-in-React-Js
Send data from child component to its parent component

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, { useState } from 'react';
import './style.css';

const ChildComponent = (props=> {
  
const { parentCallBackFunction } = props;
  const [firstNsetFirstN] = useState('');
  const [lastNsetLastN] = useState('');

  const onSubmitForm = (e=> {
    parentCallBackFunction(firstNlastN);
  };

  const onChangeFirstName = (e=> {
    setFirstN(e.target.value);
  };

  const onChangeLastName = (e=> {
    setLastN(e.target.value);
  };

  return (
    <>
      <div id="child">
        <span>Child Component</span>

        <div id="firstName">
          <label>First Name </label>
          <input
            type="text"
            name="firstname"
            placeholder="Enter First Name"
            onChange={onChangeFirstName}
          />{' '}
          <br />
        </div>
        <div id="lastName">
          <label>Last Name </label>
          <input
            type="text"
            name="lastname"
            placeholder="Enter Last Name"
            onChange={onChangeLastName}
          />
        </div>
        <button id="submit" onClick={onSubmitForm}>
          Submit values to Parent Component
        </button>
      </div>
    </>
  );
};

export default function App() {
  const [firstNamesetFirstName] = useState('');
  const [lastNamesetLastName] = useState('');

  const handleCallbackFunction = (firstnamelastname=> {
    setFirstName(firstname);
    setLastName(lastname);
  };

  return (
    <div id="parent">
      <span>Parent Component</span>
      <ChildComponent parentCallBackFunction={handleCallbackFunction} />
      <label>First Name: {firstName}</label> <br />
      <label>Last Name: {lastName}</label>
    </div>
  );
}

C) style.css

#parent {
  border1px solid black;
  width350px;
  height250px;
  margin5px;
  padding5px;
  background-colororange;
  font-weightbold;
}

#child {
  margin20px;
  margin-left20px;
  border1px solid black;
  padding3px;
  height145px;
  width300px;
  background-colorgreen;
}

#firstName {
  margin20px;
}
#lastName {
  margin20px;
}

#submit {
  margin-left15px;
}


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

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