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
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:
- Created a year ago
- Comments:16 (9 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
t
function can returnnull
, this behaviour is set by default, if you want to change it, setreturnNull
type tofalse
.I also recommend updating your i18next configuration to behave accordantly. https://www.i18next.com/overview/configuration-options#translation-defaults
This problem still occurs.
i18next 22.0.5 react-i18next 12.0.0