this.eventSource = new EventSource("http://localhost:5000/api/......")
componentDidMount() {
......
this.subscribeToServerEvent()
}
subscribeToServerEvent = () => {
this.eventSource.onmessage = e => {
try {
......
this.setState({ ..... })
} catch (e) {
console.log('error parsing server response', e)
}
}
}
this.timeout = setInterval(() => {
this.setState({ value: this.state.value + this.props.speed });
this.nanobar.go(this.state.value);
}, 1000);
componentWillUnmount() {
clearInterval(this.writer);
}
<SomeComponent hover={this.state.hover} data={item.data}/>
<Route
path='/publishedArticlePage'
render={ (props) => <PublishedArticlePage {...props} article={article} }
/>
state = { loaded: false, data: null }
const getData = () => new Promise(resolve => {
setTimeout(() => resolve( {data: 177} ), 2000)
})
...
getData().then(d => {
// что то делаем с данными, например
this.setState( { loaded: true, data: d } )
})
render() {
if (!this.state.loaded) return <Loading />
return <MyBeautifulDataComponent />
}