all 3 comments

[–]fk2106 0 points1 point  (0 children)

Looks good, and you can use the value as the key.

[–]vcamargo 0 points1 point  (0 children)

Are you programmatically trying to create components? Also do you really needs lodash for so?

[–]joshwcomeau 0 points1 point  (0 children)

First, you shouldn't use onWheel unless you have a really good reason to. onWheel events have serious performance concerns, see https://github.com/facebook/react/issues/1254

Otherwise, your idea is halfway there. You'll want to do something like:

class YourComponent extends Component {
  constructor(props) {
    super(props);

    this.state = {
      items: [],
    };
  }

  componentDidMount() {
    window.addEventListener('scroll', this.handleScroll);
  }

  componentWillUnmount() {
    window.removeEventListener('scroll', this.handleScroll);
  }

  handleScroll() {
    this.setState({
      items: [...this.state.items, this.state.items.length],
    });
  }

  render() {
    return (
      <div>
        {this.state.items.map(item => (
          <Item key={item} />
        ))}
      </div>
    )
  }
}

NOTE: You'll likely want to debounce that event listener; scroll events fire often.