I'm using setInterval
to poll requests and make my app real-time, and I think it might be the culprit why the fetch requests keep coming. This is my React component:
export default class ChatBox extends Component<ChatBoxProps, ChatBoxState> {
interval = setInterval(() => this.fetchComments(), 500)
state = {
comments: [],
content: '',
}
componentDidMount () {
this.interval = setInterval(() => this.fetchComments(), 500)
this.scrollToBottom()
}
componentDidUpdate () {
this.scrollToBottom()
}
componentWillUnmount () {
clearInterval(this.interval)
}
fetchComments = () => {
fetchComments().then(comments => {
this.setState({ comments })
})
}
}
Is there a way to prevent this behavior?