有没有一种方法可以限制从调用URL返回的响应中返回的Web数据量?

时间:2020-11-12 15:35:16

标签: react-native

有没有一种方法可以限制从调用URL响应中返回的Web数据量?

还是有一种方法可以使您滚动加载的组件时刷新以获取更多数据?

let { url, timeout } = urlConfig.getStuffc; url = `${url}?${constructQueryFromArr(statusQueryString[status], 'status')}`; httpRequest('get', url, { config: { timeout } }).then((response) => { dispatch(showSpinner(false)); } //do something

1 个答案:

答案 0 :(得分:0)

这是Flatlist的基本示例,用于在单个请求中以有限的数据实现API

export default class App extends Component {

  constructor(props) {
    super(props);
    this.page = 1;
    this.state = {
      loading: false, // user list loading
      isRefreshing: false, //for pull to refresh
      data: [], //user list
      error: ''
    }
  }

  componentWillMount() {
    this.fetchUser(this.page) //Method for API call
  }

  fetchUser(page) {
    const url = `https://api.example.com?page=${page}`;
    this.setState({ loading: true })
    axios.get(url)
      .then(res => {
        let listData = this.state.data;
        let data = listData.concat(res.data.items) . //concate list with response
        this.setState({ loading: false, data: data })
      })
      .catch(error => {
        this.setState({ loading: false, error: 'Something just went wrong' })
      });
  }

  handleLoadMore = () => {
    if (!this.state.loading) {
      this.page = this.page + 1; // increase page by 1
      this.fetchUser(this.page); // method for API call 
    }
  };

  render() {
    if (this.state.loading && this.page === 1) {
      return <View style={{
        width: '100%',
        height: '100%'
      }}><ActivityIndicator style={{ color: '#000' }} /></View>;
    }
    return (
      <View style={{ width: '100%', height: '100%' }}>
        <FlatList
          data={this.state.data}
          extraData={this.state}
          renderItem={({ item }) => (
            <View>
            ....
            </View>
          )}
          keyExtractor={(item, index) => index.toString()}
          onEndReachedThreshold={0.4}
          onEndReached={this.handleLoadMore.bind(this)}
        />
      </View>
    );
  }
}