Frontend Engineer Assignment - steeleye/recruitment-ext GitHub Wiki
Based on the code below answer the following queries:
- Explain what the simple
List
component does. - What problems / warnings are there with code?
- Please fix, optimize, and/or modify the component as much as you think is necessary.
import React, { useState, useEffect, memo } from 'react';
import PropTypes from 'prop-types';
// Single List Item
const WrappedSingleListItem = ({
index,
isSelected,
onClickHandler,
text,
}) => {
return (
<li
style={{ backgroundColor: isSelected ? 'green' : 'red'}}
onClick={onClickHandler(index)}
>
{text}
</li>
);
};
WrappedSingleListItem.propTypes = {
index: PropTypes.number,
isSelected: PropTypes.bool,
onClickHandler: PropTypes.func.isRequired,
text: PropTypes.string.isRequired,
};
const SingleListItem = memo(WrappedSingleListItem);
// List Component
const WrappedListComponent = ({
items,
}) => {
const [setSelectedIndex, selectedIndex] = useState();
useEffect(() => {
setSelectedIndex(null);
}, [items]);
const handleClick = index => {
setSelectedIndex(index);
};
return (
<ul style={{ textAlign: 'left' }}>
{items.map((item, index) => (
<SingleListItem
onClickHandler={() => handleClick(index)}
text={item.text}
index={index}
isSelected={selectedIndex}
/>
))}
</ul>
)
};
WrappedListComponent.propTypes = {
items: PropTypes.array(PropTypes.shapeOf({
text: PropTypes.string.isRequired,
})),
};
WrappedListComponent.defaultProps = {
items: null,
};
const List = memo(WrappedListComponent);
export default List;