Skip to content

Instantly share code, notes, and snippets.

@oshikryu
Last active December 6, 2019 21:26
Show Gist options
  • Save oshikryu/1d6693227e4b7efd84b2cd4e31e0a2cf to your computer and use it in GitHub Desktop.
Save oshikryu/1d6693227e4b7efd84b2cd4e31e0a2cf to your computer and use it in GitHub Desktop.
poll-interval
import React from 'react';
export class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
}
componentDidMount() {
this.poller = setInterval(() => this.callMyPollEndpoint(), 3000);
}
componentWillUnmount() {
this.poller = null; // clean up
}
callMyPollEndpoint = () => {
fetch('/my-poll-endpoint')
.then(result => result.json())
.then(result => this.setState({ items: result }));
}
render() {
const { items } = this.state;
return (
<div>
{items.map((obj) => obj}
</div>
);
}
}
export default MyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment