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

Controlled Component in React Js

Controlled Component in React Js

In every web application, handing form data is very basic requirement and if you want to handle this requirement in react Js, it provides two ways controlled component and uncontrolled component.

According to official document of React Js, it is recommended that in most of cases, controlled component should be implemented for Forms, but in this post, we will discuss about controlled  component.

In controlled component, Form data is stored in component's state and updated by "onChangeHandler"

We can use single event handler for handling all form fields, regardless of type. 

 

controlled-component-in-reactjs
Controlled Component in react js

Sample Code:

A) Class Component

 a) App.js

import React, { Component } from 'react';

class App extends Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.onChangeHandler = this.onChangeHandler.bind(this);
    this.state = { firstName: ''lastName: ''fullName: '' };
  }

  onChangeHandler(e) {
    e.preventDefault();
    this.setState({
      [e.target.name]: e.target.value,
    });
  }

  handleSubmit(e) {
    this.setState({
      fullName: this.state.firstName + ' ' + this.state.lastName,
    });
    e.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>Handling Form Data - Controlled Component</label> <br />
        <br />
        <label>
          First Name:
          <input
            value={this.state.firstName}
            type="text"
            name="firstName"
            onChange={this.onChangeHandler}
          />
        </label>{' '}
        <br />
        <label>
          Last Name:
          <input
            value={this.state.lastName}
            type="text"
            name="lastName"
            onChange={this.onChangeHandler}
          />
        </label>
        <br />
        <br />
        <input type="submit" value="Submit" /> <br /> <br />
        <label>Full Name :{this.state.fullName}</label>
      </form>
    );
  }
}

export default App;

B) Functional Component

 a) App.js

import React, { useState } from 'react';

export default function App() {
  const [statesetState] = useState({
    firstName: '',
    lastName: '',
    fullName: '',
  });

  function onChangeHandler(e) {
    e.preventDefault();
    setState({ ...state[e.target.name]: e.target.value });
  }

  function handleSubmit(e) {
    setState({ ...statefullName: state.firstName + ' ' + state.lastName });
    e.preventDefault();
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>Handling Form Data - Controlled Component</label> <br />
      <br />
      <label>
        First Name:
        <input
          value={state.firstName}
          type="text"
          name="firstName"
          onChange={onChangeHandler}
        />
      </label>{' '}
      <br />
      <label>
        Last Name:
        <input
          value={state.lastName}
          type="text"
          name="lastName"
          onChange={onChangeHandler}
        />
      </label>
      <br />
      <br />
      <input type="submit" value="Submit" /> <br /> <br />
      <label>Full Name :{state.fullName}</label>
    </form>
  );
}


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

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