I have a react component that receives props for filtering from its parent. When the parent props change I use getDerivedStateFromProps
in the child in the following code:
static getDerivedStateFromProps(props, state) {
if (props.filter === null) {
return state;
}
const { name, description } = props.filter;
ApiService.get("cards", { name: name, description: description })
.then(response => {
console.log("get request returned ", response.data);
return { cards: response.data };
}
);
}
in the console the response.data
log is the approriate array of objects. However the state does not update and the rendering function still uses the old cards
array and not the one that was received from the ApiService
response. How do I make it so the cards
array updates properly so that on the next render it will show the filtered cards?