K
K
Klaytn Docs
Search…
5-1. Blocknumber Component
src/components/BlockNumber.js:

BlockNumber component

1) Full code 2) BlockNumber component's role 3) getBlockNumber method in detail 4) Call getBlockNumber intervally

1) Full code

1
import React, { Component } from 'react'
2
3
import { cav } from 'klaytn/caver'
4
5
import './BlockNumber.scss'
6
7
/**
8
* BlockNumber component gets the current block number every 1 second.(1000ms)
9
* current block number can be fetched through caver-js library
10
* which make a connection, communicating with klaytn node.
11
* cf) If you want to connect to specific klaytn node,
12
* change 'rpcURL' config in klaytn/caver.js
13
*/
14
class BlockNumber extends Component {
15
/**
16
* BlockNumber component has a 'currentBlockNumber' state.
17
*/
18
state = {
19
currentBlockNumber: '...loading',
20
}
21
22
/**
23
* 'getBlockNumber' method works
24
* 1) get current block number from klaytn node by calling 'cav.klay.getBlockNumber()'
25
* 2) set 'currentBlockNumber' state to the value fetched from step 1).
26
*/
27
getBlockNumber = async () => {
28
const blockNumber = await cav.klay.getBlockNumber()
29
this.setState({ currentBlockNumber: blockNumber })
30
}
31
32
/**
33
* intervalId value will be populated by the value returned from `setInterval`.
34
* intervalId will be used to clear interval, preventing memory leak.
35
*/
36
intervalId = null
37
38
/**
39
* In 'componentDidMount' lifecycle, call 'getBlockNumber' method intervally.
40
*/
41
componentDidMount() {
42
this.intervalId = setInterval(this.getBlockNumber, 1000)
43
}
44
45
/**
46
* In 'componentWillUnmount' lifecycle, clear interval
47
* which calls getBlockNumber per 1000ms.
48
*/
49
componentWillUnmount() {
50
if (this.intervalId) clearInterval(this.intervalId)
51
}
52
53
/**
54
* In 'render' lifecycle, show 'currentBlockNumber' state like below:
55
* <p>Block No. {currentBlockNumber}</p>
56
*/
57
render() {
58
const { currentBlockNumber } = this.state
59
return (
60
<div className="BlockNumber">
61
<p className="BlockNumber__current">Block No. {currentBlockNumber}</p>
62
</div>
63
)
64
}
65
}
66
67
export default BlockNumber
Copied!

2) BlockNumber component's role

BlockNumber component's role is showing Klaytn's current block number. It requests the current block number to the Klaytn node by calling caver.klay.getBlockNumber() every second. This component re-renders DOM through this.setState({ currentBlockNumber: blockNumber }) upon receiving the response.

3) getBlockNumber method in detail

1
/**
2
* 'getBlockNumber' method works
3
* 1) get current block number from klaytn node by calling 'cav.klay.getBlockNumber()'
4
* 2) set 'currentBlockNumber' state to the value fetched from step 1).
5
*/
6
getBlockNumber = async () => {
7
const blockNumber = await cav.klay.getBlockNumber()
8
this.setState({ currentBlockNumber: blockNumber })
9
}
Copied!
getBlockNumber method is declared as an async function. Declaring a function as async makes dealing with asynchronous value(promise) easy. cav.klay.getBlockNumber returns a promise, and the result can be handled easily by appending await keyword.
For further information about async-await keyword, see javascript's MDN site https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
After assigning the current block number returned from cav.klay.getBlockNumber() to blockNumber, we call this.setState React API. this.setState({ currentBlockNumber: blockNumber }) literally sets the state property currentBlockNumber to blockNumber. this.setState(nextState) updates the current state and re-renders the component.
For further detail about React's this.setState and rendering mechanism, visit React's official site https://reactjs.org/docs/state-and-lifecycle.html

4) Call getBlockNumber intervally

1
/**
2
* In 'componentDidMount' lifecycle, call 'getBlockNumber' method intervally.
3
*/
4
componentDidMount() {
5
this.intervalId = setInterval(this.getBlockNumber, 1000)
6
}
Copied!
Since we want our tutorial app to show current block number lively, we call getBlockNumber every second (1000ms). We can use setInterval function to do this. setInterval(func, delay) calls the given function repeatedly with given delay. setInterval function returns an interval id which will be used to clear this interval later, so we store it to this.intervalId variable.
1
/**
2
* In 'componentWillUnmount' lifecycle, clear interval
3
* which calls getBlockNumber per 1000ms.
4
*/
5
componentWillUnmount() {
6
if (this.intervalId) clearInterval(this.intervalId)
7
}
Copied!
When the component unmounts, stop getting the current block number by removing the interval.
Last modified 1mo ago