You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 19, 2025. It is now read-only.
What is the Simple List Component in React?
Ans: The Simple List Component is a React Component which takes an array of items as a prop and renders an unordered list of those items with the option to select a single item from the list.
Upon selection, the background color of the selected list item is set to green, to indicate current selection. The Simple List Component has two components namely WrappedSingleListItem and WrappedListComponent.
The WrappedSingleListItem has four properties:
index: the index of the item in the items array
isSelected: a boolean that indicates whether the item is currently selected or not
onClickHandler: a callback function that gets called when the list item is clicked
text: the text content of the item
Whereas, the WrappedListComponent makes use of useState() and useEffect() hook to set the color of the selected list item.
Q2. What problems / warnings are there with code?
Ans:
In the WrappedSingleListItem, the onClickHandler is not being invoked properly. Instead of being called when the list item is clicked, the onClickHandler is being called immediately when the component is rendered. The correct code should be:
onClick={()=>onClickHandler(index)}
The proptypes for index and isSelected should be isRequired as well. The correct code should be:
WrappedSingleListItem.propTypes = {
index: PropTypes.number.isRequired,
isSelected: PropTypes.bool.isRequired,
onClickHandler: PropTypes.func.isRequired,
text: PropTypes.string.isRequired,
};
3.Please fix, optimize, and/or modify the component as much as you think is necessary.
Ans: The Simple List Component is a React Component which takes an array of items as a prop and renders an unordered list of those items with the option to select a single item from the list.
Upon selection, the background color of the selected list item is set to green, to indicate current selection. The Simple List Component has two components namely WrappedSingleListItem and WrappedListComponent.
The WrappedSingleListItem has four properties:
index: the index of the item in the items array
isSelected: a boolean that indicates whether the item is currently selected or not
onClickHandler: a callback function that gets called when the list item is clicked
text: the text content of the item
Whereas, the WrappedListComponent makes use of useState() and useEffect() hook to set the color of the selected list item.
Q2. What problems / warnings are there with code?
Ans:
In the WrappedSingleListItem, the onClickHandler is not being invoked properly. Instead of being called when the list item is clicked, the onClickHandler is being called immediately when the component is rendered. The correct code should be:
onClick={()=>onClickHandler(index)}
The proptypes for index and isSelected should be isRequired as well. The correct code should be:
WrappedSingleListItem.propTypes = {
index: PropTypes.number.isRequired,
isSelected: PropTypes.bool.isRequired,
onClickHandler: PropTypes.func.isRequired,
text: PropTypes.string.isRequired,
};
3.Please fix, optimize, and/or modify the component as much as you think is necessary.
The text was updated successfully, but these errors were encountered: