Im trying to filter data with multiple key value in reactjs with tsx. Cards.tsx is a parent component and ShipmentCard.tsx is a child. I'm getting Property 'toLowerCase' does not exist on type 'never'
error. I just want to return the related object based on search criteria. Can anyone let me know where I made a mistake?
Cards.tsx
class Cards extends Component {
state = {
card: [],
filter: ""
};
componentDidMount() {
this.loadShipmentList();
}
handleChange = (event: any) => {
this.setState({ filter: event.target.value });
};
loadShipmentList() {
fetch("http://localhost:3001/shipments")
.then(response => response.json())
.then(data => this.setState({ card: data }));
}
render() {
const { card, filter } = this.state;
const lowercasedFilter = filter.toLowerCase();
const filteredData = this.state.card.filter(item => {
return Object.keys(item).some(key =>
item[key].toLowerCase().includes(lowercasedFilter)
);
});
return (
<Card className="Search-Bar">
<CardContent>
<Grid container spacing={3}>
<TextField
label="Search"
onChange={this.handleChange}
/>
</Grid>
</CardContent>
</Card>
<Grid container spacing={3}>
{filteredData.map((card: any) => (
<Grid item xs={12}>
<ShipmentCard key={card.id} value={card} />
</Grid>
))}
</Grid>
);
}
}
export default Cards;
{
"shipments": [
{
"id": "123",
"name": "Heromisha",
"total": "1000",
"status": "ACTIVE",
"userId": "E222"
},
{
"id": "456",
"name": "Honda",
"total": "3000",
"status": "ACTIVE",
"userId": "E111"
}
]
}