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 · 1 comment · May be fixed by #2142
Open

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

jwueller opened this issue May 30, 2024 · 1 comment · May be fixed by #2142
Labels
bug Something isn't working

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.

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