Question 1. Explain what the simple List component does.
The List component consists of List Items that are wrapped in a component called WrappedSingleListItem. Each ListItem has a state called isSelected and values for text and index, as well as an event called onClickHandler. These components are memoized using memo(WrappedListComponent), which means that if the same arguments are passed, the result will be returned from memory.
The List component uses the map() method to iterate over an array of data and calls a function for each element. The resulting List Items are stored in an array called items. The background color of the text gets changed based on the state of isSelected. If the state is true, the text will be displayed in green else it will be displayed in red.
Question 2. What problems / warnings are there with code?
The warning "React Hook useEffect has a missing dependency: 'setSelectedIndex'. Either include it or remove the dependency array"
we get due to const [setSelectedIndex, selectedIndex] = useState(); we can remove this warning by correcting it like const [selectedIndex, setSelectedIndex] = useState(-1);
Question 3. Please fix, optimize, and/or modify the component as much as you think is necessary.
Question 1. Explain what the simple List component does.
The List component consists of List Items that are wrapped in a component called
WrappedSingleListItem
. Each ListItem has a state calledisSelected
and values for text and index, as well as an event calledonClickHandler
. These components are memoized usingmemo(WrappedListComponent)
, which means that if the same arguments are passed, the result will be returned from memory.The List component uses the
map()
method to iterate over an array of data and calls a function for each element. The resulting List Items are stored in an array called items. The background color of the text gets changed based on the state ofisSelected
. If the state is true, the text will be displayed in green else it will be displayed in red.Question 2. What problems / warnings are there with code?
We get 5 problems and one warning in this code.
Changes in the code : 1. Before
Using arrow function After correction
2. Before
Initializing the selectedIndex to -1 instead of keeping it empty. After correction
3. Before
A mismatch between the declared value of propType which is boolean and the passed value which is string creates the error here After correction
4. Before
The correct function names are arrayOf() and shape(). After correction
5. Before
The array was not initialize that is why we are getting the error here, we can remove the error by initializing the array. After
The warning "React Hook useEffect has a missing dependency: 'setSelectedIndex'. Either include it or remove the dependency array" we get due to
const [setSelectedIndex, selectedIndex] = useState();
we can remove this warning by correcting it likeconst [selectedIndex, setSelectedIndex] = useState(-1);
Question 3. Please fix, optimize, and/or modify the component as much as you think is necessary.
Link to the github repository : https://github.com/ankushkumar74/Ankush-Kumar_Front-End