Pollinterval stop on error


I am using React and was wondering is there a way to stop the polling when an error occurs?

From what I can see, the only way to achieve it, would be to set a state variable and update it when error occurs. Is this the only way?