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

Interview Question | React Js

Interview Question | React Js 

Recently, I had given one interview in React Js & one of the member from interview panel had given me following task to complete.

Task contains following requirements to complete:

a) Initially, show 7 rectangle boxes in white color & all should be clickable.

b) Once you click rectangle box, show that in green color.

c) As soon as, you click seventh rectangle box, make all rectangle boxes white again after some time interval in reverse order.

Sample Code:

A) App.js


import React, { useStateuseEffect } from 'react';
import './style.css';

const rectangle = [
  { rectId: 1rectColor: 'white' },
  { rectId: 2rectColor: 'white' },
  { rectId: 3rectColor: 'white' },
  { rectId: 4rectColor: 'white' },
  { rectId: 5rectColor: 'white' },
  { rectId: 6rectColor: 'white' },
  { rectId: 7rectColor: 'white' },
];

let count = 0;

export default function App() {
  
  const [showrectanglesetShowrectangle] = useState(rectangle);
  const [actionsetAction] = useState(false);

  const onClickDiv = (id=> {
    const newArr = showrectangle.filter((item=> {
      if (item.rectId === id) {
        item.rectColor = 'green';
        return item;
      }
      return item;
    });
    setShowrectangle(newArr);

    if (id === 7) {
      setAction(true);
      count = 7;
    } else setAction(false);
  };

  useEffect(() => {
    if (action) {
      if (count !== 0) {
        const timeOut = setTimeout(() => {
          const arr = showrectangle.filter((item=> {
            if (item.rectId === count) {
              item.rectColor = 'white';
              return item;
            }
            return item;
          });
          setShowrectangle(arr);
          count--;
        }, 1500);

        return () => {
          clearTimeout(timeOut);
        };
      } else {
        setAction(false);
      }
    }
  }, [actionshowrectangle]);

  return (
    <div>
      {showrectangle &&
        showrectangle.length > 0 &&
        showrectangle.map((item=> {
          return (
            <div
              style={{
                width: '25px',
                height: '20px',
                border: '1px solid black',
                float: 'left',
                margin: '12px',
                backgroundColor: item.rectColor,
              }}
              key={item.id}
              onClick={() => onClickDiv(item.rectId)}
            ></div>
          );
        })}
    </div>
  );
}


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