Hooks / React.useState cause flicker issue when used in onProgress
See original GitHub issueCurrent Behavior
v2 - Using hooks along with state within onProgress callback creates a re-render loop. Player flickers endlessly.
Expected Behavior
Setting a separate piece of state that the player is not dependent on should not re render ReactPlayer
Steps to Reproduce
- Create a functional component to use ReactPlayer in
- Add a piece of state using hooks pattern ex.
const [played, setPlayed] = React.useState(false);
- Create a handler function that updates state base on object passed ex.
const handleProgress = (playedObject) => {
if (!seeking) {
setPlayed(playedObject.played);
}
};
- Use handler inside onProgress method on ReactPlayer ex.
onProgress={handleProgress}
Example provided:
Uncomment setPlayed
inside handleProgress to see issue
https://codesandbox.io/s/strange-sun-cw9oq?file=/src/App.js:301-311
Environment
- URL attempting to play: any (example uses https://res.cloudinary.com/filmsupply/video/upload/ac_none,vc_auto/v1606326468/static/Home/home-hero.mp4)
- Browser: Chrome
- Operating system: macOS Big Sur
- Codesanbox example: https://codesandbox.io/s/strange-sun-cw9oq?file=/src/App.js:301-311
Other Information
If we convert this to a class component or revert to v1 the issue is not present. Also state must be set to the passed value on the progress handler (using a static value to set state does not cause flicker)
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (4 by maintainers)
Top Results From Across the Web
reactjs - useState counter flickers after clicking the button
However, clicking the button causes an immediate re-render and thus creates another timeout. This means there'll be 2 timeouts waiting, one from ...
Read more >Avoid Flashes of Content in React with useLayoutEffect
[1:55] If we refresh the page, you can see that the green modal is flashing because it's being repositioned after the DOM paints....
Read more >React Hooks cheat sheet: Best practices with examples
To prevent the user from seeing flickers of changes, you can use useLayoutEffect . The function passed to useLayoutEffect will be run before...
Read more >Hooks API Reference - React
useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on...
Read more >Useful React Hooks That You Can Use In Your Projects
React solves this problem with functional components and hooks, ... Setting states with the useState hook causes the corresponding component ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For me, it ended up being cleaner to just have a one return statement with some conditional rendering inside. That also solved the problem of it flickering because it was inside a nested function’s return statement. I’m using react-player to play video files stored in a GCP storage bucket currently. I’m using the useState React hook for states like playing, started, progress etc.
@ferrero1987 Do you have a reproducible example of this happening? Does anyone?