Last active
December 6, 2019 21:26
-
-
Save oshikryu/1d6693227e4b7efd84b2cd4e31e0a2cf to your computer and use it in GitHub Desktop.
poll-interval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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