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

useTexture(..., onLoad) callback fires incorrectly #1968

Open
jwueller opened this issue May 30, 2024 · 2 comments · May be fixed by #2142
Open

useTexture(..., onLoad) callback fires incorrectly #1968

jwueller opened this issue May 30, 2024 · 2 comments · May be fixed by #2142
Labels
bug Something isn't working Stale Inactive issue

Comments

@jwueller
Copy link

  • three version: 0.164.1
  • @react-three/fiber version: 8.16.6
  • @react-three/drei version: 9.105.6
  • node version: v22.2.0
  • npm (or yarn) version: 1.22.22

Problem description:

The onLoad callback fires every render if the callback isn't memoized, because useEffect hook depends on the function instead of the mapped texture data. On the other hand, if the callback is memoized, it will only fire once and then never again, even if the data finishes loading later.

Relevant code:

const texture = useTexture(
    'some_texture.jpg',
    () => console.log('every render'),
);

Caused by this code in useTexture:

useLayoutEffect(() => {
  onLoad?.(mappedTextures)
}, [onLoad])

Suggested solution:

useLayoutEffect(() => {
  onLoad?.(mappedTextures)
}, [mappedTextures])

I will provide a PR with this change.

@jwueller jwueller added the bug Something isn't working label May 30, 2024
@jwueller jwueller changed the title useTexture onLoad fires incorrectly useTexture(..., onLoad) callback fires incorrectly May 30, 2024
jwueller added a commit to jwueller/drei that referenced this issue May 30, 2024
@clementroche
Copy link
Contributor

Great job! I was experiencing the same actually.

Copy link

Thank you for contributing! We’re marking this issue as stale as a gentle reminder to revisit it and give it the attention it needs to move forward.

Any activity, like adding an update or comment, will automatically remove the stale label so it stays on our radar.

Feel free to reach out on Discord if you need support or feedback from the community. This issue will close automatically soon if there’s no further activity. Thank you for understanding and for being part of the project!

@github-actions github-actions bot added the Stale Inactive issue label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Inactive issue
Projects
None yet
2 participants