Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use it in React? #14

Open
Plazo-09 opened this issue Apr 29, 2024 · 3 comments
Open

How to use it in React? #14

Plazo-09 opened this issue Apr 29, 2024 · 3 comments

Comments

@Plazo-09
Copy link

No description provided.

@argyleink
Copy link
Owner

havent tried it, but i bet you could just import this, get a ref to the node you want to get scrollend from, and then use that ref to setup the event listener?

@r100-stack
Copy link

r100-stack commented Aug 28, 2024

havent tried it, but i bet you could just import this, get a ref to the node you want to get scrollend from, and then use that ref to setup the event listener?

At least when I tried it in a playground and testing it in Safari 16.6, the polyfill didn't seem to kick in. When testing in browsers that support scrollend, scrollend worked without the polyfill.

Am I missing something to make the polyfill work in React?

Code
import React from 'react';
import './App.css';
import { scrollend } from 'scrollyfills';

function App() {
  const ref = React.useRef<HTMLDivElement>(null);

  React.useEffect(() => {
    const refCurrent = ref.current;

    const listener = () => {
      console.log('scrollend');
    };

    refCurrent?.addEventListener('scrollend', listener);

    return () => {
      refCurrent?.removeEventListener('scrollend', listener);
    };
  }, []);

  return (
    <>
      <div
        id="scroll-container"
        ref={ref}
        style={{ height: '50vh', overflow: 'auto' }}
      >
        <div
          id="content"
          style={{
            height: '200vh',
            backgroundColor: 'black',
          }}
        >
          <p>Content</p>
        </div>
      </div>
    </>
  );
}

export default App;

@argyleink
Copy link
Owner

maybe there's a conflict with how React does events. perhaps they're consuming them to the root of the react app (which they've always done) but aren't forwarding the scrollend event?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants