The List component is a basic React component that displays a list of clickable items based on an array of items passed as a prop. It uses a memoized SingleListItem component to render each item and handles click events. The selected item is tracked using a state variable and reset when the list of items changes. Prop types are defined using PropTypes, and the component is optimized for performance using memoization.
Q2. What problems / warnings are there with code?
The use of useState in the provided code is incorrect. The correct syntax for using useState hook is:
where selectedIndex is the state variable that holds the current value, and setSelectedIndex is the function that can be used to
update the value of selectedIndex state variable.
'Key' props is missing in "SingleListItem " while rendering an array of components with unique items.
You can add a key prop to the SingleListItem component and set it to a unique value, such as the index
prop, to address this issue.
and in the same piece of code , isSelected is a boolean but we are assigning "index" value to it which is a number . To fix this code i modified it to isSelected={index === selectedIndex }
This code is setting the "isSelected" prop to a boolean value that indicates whether the current item is selected or not. It does this by checking if the index of the current item being mapped over is equal to the value of selectedIndex.
The use of PropTypes in the " items" propType validation is incorrect. The correct syntax for validating an
array of objects with a specific shape using PropTypes is:
This validates that the items prop is an array of objects, where each object has a text property of type string that is required (i.e., must be present).
The "isSelected " prop is intended to be a boolean value, indicating whether an item is selected or not.
setting it to null may not be a valid boolean value, and it can lead to unexpected results in the rendering of
the SingleListItem component.
To fix this, you can set a valid boolean value for " isSelected " prop, such as "false" or "0", instead of "null",
"isSelected" is expected to be a boolean value but here it is defined as "PropType.bool" , which do not enforce boolean type. To fix this issue we need to add "isRequired" so that it must be a requires boolean value
6 . "onClick={onClickHandler(index)}" would not work as expected because it would immediately invoke the
" onClickHandler" function with the index parameter when the component renders, rather than waiting for
the actual click event to occur. for example
Live Deployment Link : https://steel-eye-assignment.vercel.app/
Q1 . Explain what the simple List component does
The List component is a basic React component that displays a list of clickable items based on an array of items passed as a prop. It uses a memoized SingleListItem component to render each item and handles click events. The selected item is tracked using a state variable and reset when the list of items changes. Prop types are defined using PropTypes, and the component is optimized for performance using memoization.
Q2. What problems / warnings are there with code?
The use of useState in the provided code is incorrect. The correct syntax for using useState hook is:
const [selectedIndex, setSelectedIndex] = useState( );
where selectedIndex is the state variable that holds the current value, and setSelectedIndex is the function that can be used to update the value of selectedIndex state variable.
'Key' props is missing in "SingleListItem " while rendering an array of components with unique items. You can add a key prop to the SingleListItem component and set it to a unique value, such as the index prop, to address this issue.
and in the same piece of code , isSelected is a boolean but we are assigning "index" value to it which is a number . To fix this code i modified it to
isSelected={index === selectedIndex }
This code is setting the "isSelected" prop to a boolean value that indicates whether the current item is selected or not. It does this by checking if the index of the current item being mapped over is equal to the value of selectedIndex.
This validates that the items prop is an array of objects, where each object has a text property of type string that is required (i.e., must be present).
6 . "onClick={onClickHandler(index)}" would not work as expected because it would immediately invoke the " onClickHandler" function with the index parameter when the component renders, rather than waiting for the actual click event to occur. for example
To fix this issue :
Q3. Please fix, optimize, and/or modify the component as much as you think is necessary.
CSS