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

169 - re-render 2 - react #176

Open
kenmori opened this issue Oct 5, 2024 · 1 comment
Open

169 - re-render 2 - react #176

kenmori opened this issue Oct 5, 2024 · 1 comment

Comments

@kenmori
Copy link

kenmori commented Oct 5, 2024

App.jsx

import { useState, useDeferredValue } from 'react';

import { MovingBlock } from "./components/moving-block";
import { VerySlowComponent } from './components/very-slow-component';
import { BunchOfStuff, OtherStuffAlsoComplicated } from './components/mocks';

export default function App() {
  const [position, setPosition] = useState(150);
  const deferredPositionValue = useDeferredValue(position)

  const onScroll = (e) => {
    const calculated = getPosition(e.target.scrollTop);
    setPosition(calculated);
  };

  return (
    <div className="scrollable-block" onScroll={onScroll}>
      <MovingBlock position={deferredPositionValue} />
      <VerySlowComponent />
      <BunchOfStuff />
      <OtherStuffAlsoComplicated />
    </div>
  );
}

// just hard-coded approximation to demonstrate the re-renders problem
// not to be used in real code
const getPosition = (val) => 150 - val / 2;
@jsartisan
Copy link
Owner

@kenmori This solution is not working as expected. If you see we are not able to see the new scroll position getting updated on scroll immediately.

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

No branches or pull requests

2 participants