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

training-4-khang #71

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

khanghoanglatui172
Copy link

No description provided.

dispatch({type: 'ERROR', data: err})
})
}
const throttleApiCall = useCallback(throttle(fetchJokes, 5000),[])
Copy link
Collaborator

@vanbui1995 vanbui1995 Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetchJokes is created new every render time, so you don't put it as dependencies of useCallback, which means the throttle function always applies for the first func created?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, throttle or debounce function should not apply for the function can be created many times

searchCountry(e.target.value);
}

const searchCountry = useCallback(debounce(fetchCountry, 1000),[])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with training 1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleSearch call to latest searchCountry created, but the debounce function only apply for 1st fetchCountry created

@khanghoanglatui172
Copy link
Author

@vanbui1995 I have already update your reviews. Could you check it?

})
}

const handleGetMore = throttle(() => {
Copy link
Collaborator

@vanbui1995 vanbui1995 Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still wrong, the arrow function will be created new for every render, handleGetMore function also created new

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to memorize the fetchJokes first, should not let create every time we got render -> hint: use callback dependencies [setLoading, dispatch] -> it will be same [], because setLoading & dispatch are not changed at anytime

then memorize the handleGetMore

}
}, [search])

const handleSetSearch = debounce((keyword: string) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issue here

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

Successfully merging this pull request may close these issues.

2 participants