У меня есть конструктор, метод get, componentDidMount и componentWillUnmount. Я хочу просто прослушать событие прокрутки и, в соответствии с этим, вызвать метод get. Если прокрутка находится внизу страницы, метод get вызывается один раз. Это все. Первый вызов componentDidmount () работает один раз, но когда я прокручиваю вниз, метод get работает два раза. Я не хочу, чтобы он выполнялся более одного раза.
Это мой код:
constructor(props) {
super(props);
cursor = -1
id = auth().currentUser.providerData[0].uid
this.state = {
hits: [],
isLoading: false,
over: false,
firstCall: true
};
this.get = this.get.bind(this)
this.handleScroll = this.handleScroll.bind(this)
}
get() {
this.setState({ isLoading: true });
fetch("/1.1/followers/list.json?user_id=" + id + "&cursor=" + cursor + "", {
headers: {
'Authorization': 'MyToken',
}
}).then(response => response.json())
.then(data => {
if (data.next_cursor == 0) {
this.setState({ isLoading: false })
this.setState({ over: true })
} else {
this.setState({ hits: this.state.hits.concat(data.users), isLoading: false })
cursor = data.next_cursor
console.log(data.next_cursor)
}
}).catch(error => {
return
})
}
componentDidMount() {
this.get()
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll(event) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
this.get()
}
}
А это вывод моей консоли.
1634251341094964000 ----> один раз
1614497980820334000 ----> два раза
1579177573029464600 ----> два раза
. , .
Они поступают из console.log (data.next_cursor) в функции get.