question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TS: Can no longer assign translations directly in a setState() after version 12.0.0 bump

See original GitHub issue

🐛 Bug Report

Prior to v12.0.0 we could assign our translation directly to a setState of the use state hook like so:

const [state, setState] = useState('');

function myFunction() {
 setState(t('my-translated-text'));
}

After the version bump from v11 to v12 we unfortunately face the below error: Argument of type 'DefaultTFuncReturn' is not assignable to parameter of type 'SetStateAction<string>'.

To Reproduce

image

Expected behavior

I would expect to be able to pass the TFunction directly to a setStateAction as in previous version.

We have found a workaround where can remove the error by wrapping the TFunction in a template string like so:

const [state, setState] = useState('');

function myFunction() {
 setState(`${t('my-translated-text')}`);
}

Is this now the intended way to pass translations to a setState hook after the latest type safety improvements? Please feel free to point me towards the new documentation about hook interaction in case I missed it.

Your Environment

  • runtime version: node v18
  • i18next version: 22.0.4
  • os: Mac

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
adraicommented, Nov 15, 2022

t function can return null, this behaviour is set by default, if you want to change it, set returnNull type to false.

// i18next.d.ts
import 'i18next';

declare module 'i18next' {
  interface CustomTypeOptions {
    returnNull: false;
    ...
  }
}

I also recommend updating your i18next configuration to behave accordantly. https://www.i18next.com/overview/configuration-options#translation-defaults

i18next.init({
  returnNull: false,
   ...
});
4reactions
swkang0513commented, Nov 15, 2022

should be fixed with i18next v22.0.5

This problem still occurs.

i18next 22.0.5 react-i18next 12.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does calling react setState method not mutate the state ...
From React's documentation: setState() does not immediately mutate this.state but creates a pending state transition. Accessing this.state after calling ...
Read more >
React setState does not immediately update the state - Medium
Think of setState() as a request to update the component. Reading state right after calling setState() a potential pitfall. useState React hook.
Read more >
Why is it so difficult to modify a deeply nested state in React?
Is that really so that solo React can not tackle deeply nested state updates with ... It means that it doesn't modify the...
Read more >
Accessing React State right after setting it - DEV Community ‍ ‍
console.log doesn't have access to updated state value even ... As setState is an operation, you can just wait till the value is...
Read more >
react-i18next Recent Issues - IssueHint
TS : Can no longer assign translations directly in a setState() after version 12.0.0 bump, closed, 16, 2022-11-02, 2022-12-04. Typesafe does not work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found