Skip to content

Commit

Permalink
add use callback once
Browse files Browse the repository at this point in the history
  • Loading branch information
ras1 committed Sep 1, 2023
1 parent e46b4dd commit d498d67
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/pages/ShufflePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ function ShufflePlayer() {
const [hideDescription, setHideDescription] = useState(true)

const [videosResult, fetchPlaylistVideos] = useVideoHook(selectedPlaylistIds)
useEffect(loadPlaylists, [])
useEffect(pickNextVideo, [videosResult])
useEffect(() => {
localStorage.setItem("selectedPlaylistIds", JSON.stringify(selectedPlaylistIds));
}, [selectedPlaylistIds]);

useCallbackOnce(loadPlaylists)

function useCallbackOnce(callbackFunction, condition = true) {
const isCalledRef = React.useRef(false);

React.useEffect(() => {
if (condition && !isCalledRef.current) {
isCalledRef.current = true;
callbackFunction();
}
}, [callbackFunction, condition]);
}

function loadPlaylists() {
axios.get(AppConstants.APIEndpoints.TRACKED_PLAYLISTS)
.then(response => {
Expand Down

0 comments on commit d498d67

Please sign in to comment.