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

chore: resolve react-hooks/exhaustive-deps warnings #1991

Merged
merged 14 commits into from
Mar 30, 2021
Merged

Conversation

borisyordanov
Copy link
Contributor

@borisyordanov borisyordanov commented Mar 18, 2021

FX-1777

Description

Describe the changes and motivations for the pull request.

How to test

  • run yarn lint --rule "{react-hooks/exhaustive-deps: error}" --quiet, it should return 0 errors

Screenshots

Before. After.
Insert screenshots or screen recordings Insert screenshots or screen recordings

Review

PR commands

List of available commands:

  • @toptal-bot run all - Run whole pipeline
  • @toptal-bot run build - Check build
  • @toptal-bot run visual - Run visual tests
  • @toptal-bot run deploy:documentation - Deploy documentation
  • @toptal-bot run package:alpha-release - Release alpha version

@borisyordanov borisyordanov requested a review from a team as a code owner March 18, 2021 08:19
@borisyordanov borisyordanov changed the title fix: react-hooks/exhaustive-deps chore: resolve react-hooks/exhaustive-deps warnings Mar 18, 2021
@borisyordanov borisyordanov marked this pull request as draft March 18, 2021 08:41
@borisyordanov borisyordanov marked this pull request as ready for review March 18, 2021 09:24
@borisyordanov
Copy link
Contributor Author

@toptal-bot run visual

const handleChangeDebounced = useCallback(
debounce(async (inputValue: string) => {
const newOptions = await loadOptions(inputValue.trim().toLowerCase())
const handleChangeDebounced = debounce(async (inputValue: string) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not make sense to me. Debounce will be run on every render and hence the effect will be lost. If this is just to delay the execution, maybe we should rather use delay function here?

const showErrorNotification = (errors: SubmissionErrors) => {
if (typeof errors === 'string') {
showError(errors, undefined, { persist: true })
const showErrorNotification = useCallback(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we struggle here for the reference identity but later do this

        onSubmit={handleSubmit}
        decorators={[...decorators, scrollToErrorDecorator]}

decorators always get a new array and render happens all the time.

I think we should remove all useCallback if there are no performance problems reports

Copy link
Collaborator

Choose a reason for hiding this comment

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

I probably agree, it will hide the issue

@@ -84,6 +84,7 @@ export const useNodes = (
// we have to render nodes twice: first for the initial showing data, and the second one — with the correct positions.
const nodes = useMemo<DynamicPointNode[]>(() => {
return updateNodesYPosition(dynamicNodes)
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of the comment above: // we have to render nodes twice: first for the initial showing data, and the second one — with the correct positions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we maybe fix it properly by doing a hook that would run a function on the second render?

@@ -25,6 +25,7 @@ export const useZoom = <ZoomRefElement extends ZoomedElementBaseType>({
const [initialized, setInitialized] = useState(false)
const zoom = useMemo(
() => d3.zoom<ZoomRefElement, unknown>().scaleExtent(scaleExtent),
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because eslint adds ZoomRefElement as a dependency

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is not there a way to fix it properly? It's just type, right?

)
setLoading(false)
setOptions(newOptions)
}, 500)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above. This is not debounced any more

@@ -8,14 +8,16 @@ export interface ReferenceObject {
const useWidthOf = <T extends ReferenceObject>(element: T | null) => {
const [menuWidth, setMenuWidth] = useState<string | undefined>()

const parent = element?.offsetParent
Copy link
Collaborator

Choose a reason for hiding this comment

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

is parent accurate name?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rename it

Copy link
Contributor

Choose a reason for hiding this comment

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

is parentElement okay?

@@ -186,6 +186,7 @@ export const useScreens = <T = unknown>() => {

return defaultValue
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because otherwise, eslint adds T as a dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a bug with eslint. I updated the dependency and it seems ok now
facebook/react#19751 (comment)

Copy link
Collaborator

@denieler denieler left a comment

Choose a reason for hiding this comment

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

A bit dangerous change. It might break some behaviour in some hooks 😃 But probably better to fix this rule, break and fix later 👍

const showErrorNotification = (errors: SubmissionErrors) => {
if (typeof errors === 'string') {
showError(errors, undefined, { persist: true })
const showErrorNotification = useCallback(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I probably agree, it will hide the issue

}
}, [selectedNode, selectedNode?.data])
Copy link
Collaborator

Choose a reason for hiding this comment

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

this was interesting 😁

packages/picasso-lab/src/TreeView/useNodes.ts Show resolved Hide resolved
Comment on lines 90 to 89
if (!initialized) {
return updateNodesYPosition(dynamicNodes)
}

return updateNodesYPosition(dynamicNodes)
Copy link
Collaborator

Choose a reason for hiding this comment

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

are those 2 different? look the same

@@ -8,14 +8,16 @@ export interface ReferenceObject {
const useWidthOf = <T extends ReferenceObject>(element: T | null) => {
const [menuWidth, setMenuWidth] = useState<string | undefined>()

const parent = element?.offsetParent
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rename it

Copy link
Contributor

@havenchyk havenchyk left a comment

Choose a reason for hiding this comment

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

I would fix what Oleksandr found on Form and we're good to go 👍

@toptal-devbot
Copy link
Collaborator

🎉 Last commit is successfully deployed 🎉

Demo is available on:

Your davinci-bot 🚀

@borisyordanov borisyordanov merged commit bc0f242 into master Mar 30, 2021
@borisyordanov borisyordanov deleted the fix-deps branch March 30, 2021 13:34
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.

6 participants