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

refactor: remove unneeded object #1286

Merged
merged 1 commit into from
Mar 26, 2021
Merged

refactor: remove unneeded object #1286

merged 1 commit into from
Mar 26, 2021

Conversation

SCjona
Copy link
Contributor

@SCjona SCjona commented Mar 25, 2021

Minor optimizations

Works by switching to useState(() => initialValue) and setState((prevState) => nextState)

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • documentation is changed or added

@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.205% when pulling 740c4e3 on TQjona:master into ffbe35e on i18next:master.

@jamuhl
Copy link
Member

jamuhl commented Mar 25, 2021

Will now unneeded -> does it make a difference?

@SCjona
Copy link
Contributor Author

SCjona commented Mar 25, 2021

This saves a few bytes (~16) in bundle size and some heap memory (approx. 64 bytes per element using useTranslation) due to the object being removed. Theoretically you should also get better performance due to less references on the heap (less work for the garbage collector) and less operations in the JS code, but in the real world those improvements are too small to measure. React can't even handle the amount of elements required to make this effect visible.

I'd still consider merging the PR due to the widespread use of this library, although I wouldn't create an extra release with just this small change.

}
const [t, setT] = useState(getT()); // seems we can't have functions as value -> wrap it in obj
Copy link
Member

Choose a reason for hiding this comment

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

what was the reason for this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume the original author of the code just tried useState(getT()) with getT() returning the i18n.t function. React detected that the parameter passed to useState was a function and thought it was a lazy-init function (useState(() => initialValue)). So it was calling i18n.t with no args and used the return value as initial value, which was probably null or undefined.

Basically to store a function in a state you have to have a function which returns the function, e.g. useState(() => (args) => console.log(args)) and setState(() => (args) => console.log(args))

@adrai
Copy link
Member

adrai commented Mar 25, 2021

lgtm

@jamuhl jamuhl merged commit e83da8a into i18next:master Mar 26, 2021
@jamuhl
Copy link
Member

jamuhl commented Mar 26, 2021

thank you...was published in react-i18next@11.8.12

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.

4 participants