I used to work with functional components only so I am having a problem with class components life cycles.
I need to rerender the class when the params id changes. I was able to change the params id with history.push(home/items/${id}
) but as the route changes the page is not rerendering.
const getItems = () => {
client
.query({
query: GET_ITEMS,
})
.then((result) =>
this.setState({ items: result.data.items })
);
};
componentDidMount() {
let { type } = this.props.match.params;
this.setState({ type });
getItems();
}
componentDidUpdate(prevState) {
let { type } = this.props.match.params;
if (type !== prevState.type) {
this.setState({ type });
}
}
render() {
const { type } = this.props.match.params;
return (
<ul className={stl.list}>
{this.state.items?.map(({ name }) => (
<div>
<li
style={this.state.type === name ? activeTabStyle : {}}
onClick={() => this.handleTabClick(name)}
>
{name}
</li>
{this.state.type === name && <div className={stl.underline}></div>}
</div>
))}
</ul>
);
}
I tried to use componentDidUpdate which I think is the correct equivalent for useEffect(() => {}, [dependants]) but it gave me error saying max limit is met
I included componentDidUpdate which is giving error Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
My componentDidUpdate function might be wrong, because I need to rerender based on params id not prevState, prevProps. So I tried to set the params id to state and use componentDidUpdate.