Q1. Explain what the simple List component does.
Ans:
The simple List component is a React component that displays a list of items. It receives an array of items as a prop, and for each item, it renders a SingleListItem component.
Each SingleListItem component displays the text of the item and changes its background color based on whether it is selected ( GREEN IN THIS CASE ) or not ( RED IN THIS CASE ).
When a SingleListItem component is clicked, it calls a click handler that updates the state of the WrappedListComponent to set the index of the clicked item as the selected index.
Q2. What problems / warnings are there with code?
Ans:
The isSelected prop of the SingleListItem component should be a boolean, but it is being passed the selectedIndex state, which is a number. This means that the background color of the list items will always be green (truthy) unless selectedIndex is explicitly set to false or null.
In the WrappedListComponent, the useState hook is not being used correctly. setSelectedIndex should be used to update the selectedIndex state variable, but instead, setSelectedIndex is being assigned a value of null in the useEffect hook. This will cause the selectedIndex state variable to be null initially and after the items prop changes. Also, selectedIndex should have an initial value of -1 instead of undefined/null.
The PropTypes for the items prop of WrappedListComponent are incorrect. It should be PropTypes.arrayOf(PropTypes.shape({text: PropTypes.string.isRequired})) instead of PropTypes.array(PropTypes.shapeOf({text: PropTypes.string.isRequired})).
The onClickHandler prop of the SingleListItem component is not being passed the index parameter correctly. It should be passed as a function like () => onClickHandler(index) instead of onClickHandler(index).
Q3. Please fix, optimize, and/or modify the component as much as you think is necessary.
Ans:
In the optimized version, the following changes have been made:
The isSelected prop of the SingleListItem component has been fixed to correctly pass a boolean value.
The setSelectedIndex hook is being used correctly and the initial value of selectedIndex is -1.
The PropTypes for the items prop have been fixed.
The onClickHandler prop of the SingleListItem component has been renamed to onClick and fixed to pass the index parameter correctly using a useCallback hook.
A key prop ( key={index} ) has been added to the SingleListItem component to avoid React warnings.
Deployed Link: https://steeleye-react-assignment.web.app
GitHub Repository Link: https://github.com/krishanpareek809/Krishan-Pareek_Front-End
Q1. Explain what the simple List component does. Ans:
The simple List component is a React component that displays a list of items. It receives an array of items as a prop, and for each item, it renders a SingleListItem component.
Each SingleListItem component displays the text of the item and changes its background color based on whether it is selected ( GREEN IN THIS CASE ) or not ( RED IN THIS CASE ).
When a SingleListItem component is clicked, it calls a click handler that updates the state of the WrappedListComponent to set the index of the clicked item as the selected index.
Q2. What problems / warnings are there with code? Ans:
The isSelected prop of the SingleListItem component should be a boolean, but it is being passed the selectedIndex state, which is a number. This means that the background color of the list items will always be green (truthy) unless selectedIndex is explicitly set to false or null.
In the WrappedListComponent, the useState hook is not being used correctly. setSelectedIndex should be used to update the selectedIndex state variable, but instead, setSelectedIndex is being assigned a value of null in the useEffect hook. This will cause the selectedIndex state variable to be null initially and after the items prop changes. Also, selectedIndex should have an initial value of -1 instead of undefined/null.
The PropTypes for the items prop of WrappedListComponent are incorrect. It should be PropTypes.arrayOf(PropTypes.shape({text: PropTypes.string.isRequired})) instead of PropTypes.array(PropTypes.shapeOf({text: PropTypes.string.isRequired})).
The onClickHandler prop of the SingleListItem component is not being passed the index parameter correctly. It should be passed as a function like () => onClickHandler(index) instead of onClickHandler(index).
Q3. Please fix, optimize, and/or modify the component as much as you think is necessary. Ans:
In the optimized version, the following changes have been made:
The isSelected prop of the SingleListItem component has been fixed to correctly pass a boolean value.
The setSelectedIndex hook is being used correctly and the initial value of selectedIndex is -1.
The PropTypes for the items prop have been fixed.
The onClickHandler prop of the SingleListItem component has been renamed to onClick and fixed to pass the index parameter correctly using a useCallback hook.
A key prop ( key={index} ) has been added to the SingleListItem component to avoid React warnings.