question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get ref= to work properly

See original GitHub issue

Hi There! Big fan of the library so far. I’m having an issue getting the ref logic to work for the scrollTo functionality (trying to mimic ScrollSync functionality from react-virtualized).

Also, please let me know if this is a poor use of issues.

React Version: “16.4.2” React Window: “1.1.2”

The <Grid> component is calling this.syncHeaderScroll in the onScroll prop.

I was hoping syncHeaderScroll would look something like this:

  syncHeaderScroll = ({ scrollLeft }) => {
    if (this.listRef.current) {
      this.listRef.current.scrollTo(scrollLeft)
    }
  }

I’m trying to make sure I’m not missing anything. When I console.log this.listRef.current I’m just getting null.

Code snippet here:

export class ActivityStream extends PureComponent {
  listRef = React.createRef()

  syncHeaderScroll = ({ scrollLeft }) => {
    console.log("this.listRef.current", this.listRef.current)
  }

  render() {
    return (
      <Fragment>
        <List
          direction="horizontal"
          ref={this.listRef}
          height={HEADER_HEIGHT}
          itemCount={ACTIVITY_STREAM_HEADERS.length}
          itemData={ACTIVITY_STREAM_HEADERS}
          itemSize={index => ACTIVITY_STREAM_COLUMN_WIDTHS[index]}
          width={tableWidth}
        >
          {props => <HeaderRenderer filters={filters} onOpenFilter={this.handleOpen} {...props} />}
        </List>

        <Grid
          columnCount={ACTIVITY_STREAM_HEADERS.length}
          columnWidth={index => ACTIVITY_STREAM_COLUMN_WIDTHS[index]}
          estimatedRowHeight={DEFAULT_ROW_HEIGHT}
          height={windowHeight - 250}
          itemData={itemData}
          overscanCount={10}
          rowCount={rowData.length}
          rowHeight={index => {
            const { first_session_activity, last_session_activity } = rowData[index]

            if (first_session_activity && last_session_activity) {
              return DEFAULT_ROW_HEIGHT + DIVIDER_BOTTOM_MARGIN + BORDER_WIDTH * 2
            }

            if (first_session_activity) {
              return DEFAULT_ROW_HEIGHT + DIVIDER_BOTTOM_MARGIN + BORDER_WIDTH
            }

            if (last_session_activity) {
              return DEFAULT_ROW_HEIGHT + BORDER_WIDTH
            }

            return DEFAULT_ROW_HEIGHT
          }}
          width={tableWidth}
          onScroll={this.syncHeaderScroll}
        >
          {ItemRenderer}
        </Grid>
      </Fragment>
    )
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
danvkcommented, Sep 16, 2020

FWIW I had a similar issue which I resolved by putting ref.current in my deps list for useEffect rather than ref.

0reactions
levrikcommented, Mar 20, 2020

@gautamits That’s what I ended up with back then already as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why I can't get element by ref using this.$refs in Vue.js 2?
Hi. I have a problem because I need to name the ref as unique because this is a component that will reuse again....
Read more >
Understanding refs in Vue - LogRocket Blog
Refs are Vue.js instance properties used to register a reference to HTML elements or child elements in the template of your application.
Read more >
Get (a ref) not working on array of components. Quality of life ...
Greetings,. It just doesn't work anymore. Before 4.16 Get always returns a ref. Now if I click 'Change to return a reference' it...
Read more >
Refs and the DOM - React
Refs and Function Components ... If you want to allow people to take a ref to your function component, you can use forwardRef...
Read more >
Focus management with Vue refs - Learn web development
This should take no inputs. Note that you cannot use an arrow function here since we need access to this to access our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found