Skip to content

undefined: Duplicate declaration "initLoading" #148

@endersaka

Description

@endersaka

Microsoft Windows 10 Pro, 10.0.19043 Build 19043
Visual Studio Code 1.62.0 (system setup)
glean v5.2.2

I attempted to refactor a class component into a functional component. At the end of the process glean presents an alert displaying the text: undefined: Duplicate declaration "initLoading". You can see it in the following images.

vscode-glean_2021-11-11 114718
vscode-glean_2021-11-11 114634
vscode-glean_2021-11-11 114601
vscode-glean_2021-11-11 114811

The component code (slightly modified from Ant Design documentation example Load More).

import React from 'react';
import { List, Avatar, Button, Skeleton } from 'antd';
import reqwest from 'reqwest';
import 'antd/dist/antd.css';

const count = 20;
const fakeDataUrl = `https://randomuser.me/api/?results=${count}&inc=name,email,picture,login&noinfo`;

class LoadMoreList extends React.Component {
  state = {
    initLoading: true,
    loading: false,
    data: [],
    list: [],
  };

  componentDidMount() {
    this.getData(res => {
      this.setState({
        initLoading: false,
        data: res.results,
        list: res.results,
      });
    });
  }

  getData = callback => {
    reqwest({
      url: fakeDataUrl,
      type: 'json',
      method: 'get',
      contentType: 'application/json',
      success: res => {
        callback(res);
      },
    });
  };

  onLoadMore = () => {
    this.setState({
      loading: true,
      list: this.state.data.concat(
        [...new Array(count)].map(() => ({ loading: true, name: {}, picture: {} })),
      ),
    });
    this.getData(res => {
      const data = this.state.data.concat(res.results);
      this.setState(
        {
          data,
          list: data,
          loading: false,
        },
        () => {
          // Resetting window's offsetTop so as to display react-virtualized demo underfloor.
          // In real scene, you can using public method of react-virtualized:
          // https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
          window.dispatchEvent(new Event('resize'));
        },
      );
    });
  };

  render() {
    const { initLoading, loading, list } = this.state;
    const loadMore =
      !initLoading && !loading ? (
        <div
          style={{
            textAlign: 'center',
            marginTop: 12,
            height: 32,
            lineHeight: '32px',
          }}
        >
          <Button onClick={this.onLoadMore}>loading more</Button>
        </div>
      ) : null;

    return (
      <List
        className="demo-loadmore-list"
        loading={initLoading}
        itemLayout="horizontal"
        loadMore={loadMore}
        dataSource={list}
        renderItem={item =>
          console.log(item) || (
            <List.Item
              actions={[<a key="list-loadmore-edit">edit</a>, <a key="list-loadmore-more">more</a>]}
            >
              <Skeleton avatar title={false} loading={item.loading} active>
                <List.Item.Meta
                  avatar={<Avatar src={item.picture.large} />}
                  title={item.name.title + ' ' + item.name.first + ' ' + item.name.last}
                  description={item.login.username}
                />
                <div>content</div>
              </Skeleton>
            </List.Item>
          )
        }
      />
    );
  }
}

export default LoadMoreList;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions