steeleye / recruitment-ext

13 stars 25 forks source link

Uttam-Gupta_Front-End #182

Open UTTAMGUPTA2712 opened 1 year ago

UTTAMGUPTA2712 commented 1 year ago

Uttam-Gupta_Front-End

Q1. Explain what the simple List component does.

The List component is a React component that represents a list of items. It uses the SingleListItem component as a memoized sub-component to render each individual item in the list. The ListComponent manages the state for the list, including the selectedIndex state using the useState hook, and handles the click event on items with the handleClick function.

The List component has the following key features:

SingleListItem component:

This is a memoized sub-component that represents a single item in the list. It receives props such as index, isSelected, onClickHandler, and text from the parent component. These props are used to display the item's text, manage its selected state, and handle click events. The SingleListItem component is memoized using the memo higher-order component (HOC) to optimize performance by preventing unnecessary re-renders when the props haven't changed.

ListComponent state management:

The ListComponent uses the useState hook to manage the selectedIndex state, which keeps track of the index of the currently selected item in the list. The useState hook returns an array with two elements: the current state value and a function to update the state. The selectedIndex state is initialized to null by default.

ListComponent useEffect hook:

The ListComponent uses the useEffect hook to reset the selectedIndex state to null whenever the items prop changes. The useEffect hook is used for side effects, such as running code after rendering, and it takes a callback function as its first argument that gets executed when the dependencies provided in the second argument change. In this case, the items prop is provided as a dependency, so whenever the items prop changes, the callback function is executed, resetting the selectedIndex state to null.

ListComponent handleClick function:

The ListComponent has a handleClick function that takes the index of the clicked item as an argument. When an item is clicked, the handleClick function is called with the index of the clicked item, and it updates the selectedIndex state with the new index value using the setSelectedIndex function, which is the second element returned by the useState hook. This causes a re-render of the ListComponent with the updated selectedIndex state.

Rendering of SingleListItem components:

The ListComponent maps over the items prop using the map function and renders SingleListItem components for each item in the list. It passes the necessary props such as onClickHandler, text, index, and isSelected to SingleListItem components. The index and text props are passed directly from the items prop, while the onClickHandler is set to the handleClick function of ListComponent and the isSelected prop is set to the selectedIndex state value.

PropTypes:

The ListComponent and SingleListItem components use the PropTypes library to define the expected prop types. PropTypes are used to validate that the props passed to the components have the correct data types and are required or not. For example, items prop of ListComponent is defined as an array of objects with a required text prop of string data type. This helps to catch potential prop type errors during development.

Q2. What problems / warnings are there with code?

Q3. Please fix, optimize, and/or modify the component as much as you think is necessary.

import React, { useState, useEffect, memo } from 'react';
import PropTypes from 'prop-types';

const SingleListItem = memo(({ 
    index, 
    isSelected, 
    onClickHandler, 
    text }) => {
  return (
    <li
      style={{ backgroundColor: isSelected ? 'green' : 'red' }}
      onClick={() => onClickHandler(index)}
    >
      {text}
    </li>
  );
});

SingleListItem.propTypes = {
  index: PropTypes.number,
  isSelected: PropTypes.bool,
  onClickHandler: PropTypes.func.isRequired,
  text: PropTypes.string.isRequired,
};

const List = memo(({ items }) => {
  const [selectedIndex, setSelectedIndex] = useState(null);

  useEffect(() => {
    setSelectedIndex(null);
  }, [items]);

  const handleClick = (index) => {
    setSelectedIndex(index);
  };

  return (
    <ul style={{ textAlign: 'left' }}>
      {items.map((item, index) => (
        <SingleListItem
          key={index}
          onClickHandler={handleClick}
          text={item.text}
          index={index}
          isSelected={index === selectedIndex}
        />
      ))}
    </ul>
  );
});

List.propTypes = {
  items: PropTypes.arrayOf(PropTypes.shape({
      text: PropTypes.string.isRequired,
    })
  ).isRequired,
};
List.defaultProps = { 
    items: [
      { text: "Uttam" },
      { text: "12002071" },
      { text: "LPU" },
      { text: "B-Tech" },
      { text: "CSE" },

    ],
  };

export default List;