=> This code creates a React component called 'List', which receives an array of objects named 'items' as a prop. Each 'item' object must have a 'text' parameter of type string. The 'List' component iterates through the 'items' array using the 'map' function and renders a 'SingleListItem' component for each 'item' object. The 'SingleListItem' component receives props such as 'onClickHandler', 'text', 'index', and 'isSelected' from the 'List' component. The 'index' prop is used to identify each list item uniquely, while 'isSelected' is used to highlight the selected list item with a green background. By default, all list items have a red background color. To avoid unnecessary re-renders, the 'SingleListItem' component is memoized using the 'memo' function from React. This ensures that the component only re-renders when the value of the 'isSelected' prop changes, even if the user double-clicks on a list item. Overall, the 'List' component creates a list of items using the memoized 'SingleListItem' component, which efficiently renders each list item with the appropriate text and updates the selected item's background color.
2.) What problems / warnings are there with code?
a. Uncaught TypeError: PropTypes.shapeOf is not a function, the propTypes declaration for the items prop in the WrappedListComponent component is wrong.
c. The onClickHandler property in WrappedSingleList Item is called incorrectly, the onClick handler is being called immediately when the component is rendered instead of when the component is clicked.
Given Code:
d. Uncaught TypeError: setSelectedIndex is not a function in the code given, useState hook returns an array of two elements, with the first being the state value, and the second being the function that updates the state value. However, in the WrappedListComponent code, the setSelectedIndex function is being utilized as the state value itself, rather than the function to update the state value which is wrong.
e. The isSelected prop in the WrappedSingleListItem component is expected to be a boolean but is being passed the selectedIndex state value, which is a number.
f. Whenever we map the items there should be a unique key for each child but here it is not defined as a prop for each child, which results in a warning in console.
Deployed Link : https://gourav-steeleye-frontend.netlify.app/
Repository Link : https://github.com/Gourav-Makkar/Gourav_Frontend
1.) Explain what the simple List component does.
=> This code creates a React component called 'List', which receives an array of objects named 'items' as a prop. Each 'item' object must have a 'text' parameter of type string. The 'List' component iterates through the 'items' array using the 'map' function and renders a 'SingleListItem' component for each 'item' object. The 'SingleListItem' component receives props such as 'onClickHandler', 'text', 'index', and 'isSelected' from the 'List' component. The 'index' prop is used to identify each list item uniquely, while 'isSelected' is used to highlight the selected list item with a green background. By default, all list items have a red background color. To avoid unnecessary re-renders, the 'SingleListItem' component is memoized using the 'memo' function from React. This ensures that the component only re-renders when the value of the 'isSelected' prop changes, even if the user double-clicks on a list item. Overall, the 'List' component creates a list of items using the memoized 'SingleListItem' component, which efficiently renders each list item with the appropriate text and updates the selected item's background color.
2.) What problems / warnings are there with code?
a. Uncaught TypeError: PropTypes.shapeOf is not a function, the propTypes declaration for the items prop in the WrappedListComponent component is wrong.
Given Code:
Modified Code:
b. The default value of items is set NULL which can lead to issues as we cannot use map function over that. Given Code:
Modified Code:
c. The onClickHandler property in WrappedSingleList Item is called incorrectly, the onClick handler is being called immediately when the component is rendered instead of when the component is clicked. Given Code:
Modified Code:
d. Uncaught TypeError: setSelectedIndex is not a function in the code given, useState hook returns an array of two elements, with the first being the state value, and the second being the function that updates the state value. However, in the WrappedListComponent code, the setSelectedIndex function is being utilized as the state value itself, rather than the function to update the state value which is wrong.
Given Code:
Modified Code:
e. The isSelected prop in the WrappedSingleListItem component is expected to be a boolean but is being passed the selectedIndex state value, which is a number.
Given Code:
Modified Code:
f. Whenever we map the items there should be a unique key for each child but here it is not defined as a prop for each child, which results in a warning in console.
Given Code:
Modified Code:
Q3.) Please fix, optimize, and/or modify the component as much as you think is necessary.