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

Uncontrolled Component in React Js

Uncontrolled 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 uncontrolled  component.

Basic Difference between controlled and uncontrolled component:

a) In a controlled component, form data is handled by a React component.

b) In a uncontrolled component, form data is handled by the DOM itself.

uncontrolled-component-in-react-js
Uncontrolled Component in React Js

Default Values:

With an uncontrolled component, if you want to specify the initial value, you can specify a defaultValue attribute instead of value.

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.inputFirstName = React.createRef();
    this.inputLastName = React.createRef();
    this.state = {fullName: ''}
  }

  handleSubmit(event) {
    const name = this.inputFirstName.current.value + ' ' + 
    this.inputLastName.current.value;

    this.setState({fullName: name});
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Handling Form Data - Uncontrolled Component
        </label> <br /><br />
        <label>
          First Name:
          <input defaultValue="vikas" type="text" ref={this.inputFirstName} />
        </label> <br />
        <label>
          Last Name:
          <input defaultValue="gore" type="text" ref={this.inputLastName} />
        </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, { useRefuseState } from 'react';

export default function App() {
  const inputFirstName = useRef('');
  const inputLastName = useRef('');
  const [fullNamesetFullName] = useState('');

  function handleSubmit(event) {
    const name =
      inputFirstName.current.value + ' ' + inputLastName.current.value;

    setFullName(name);
    event.preventDefault();
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>Handling Form Data - Uncontrolled Component</label> <br />
      <br />
      <label>
        First Name:
        <input defaultValue="vikas" type="text" ref={inputFirstName} />
      </label>{' '}
      <br />
      <label>
        Last Name:
        <input defaultValue="gore" type="text" ref={inputLastName} />
      </label>
      <br />
      <br />
      <input type="submit" value="Submit" /> <br /> <br />
      <label>Full Name :{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