hossein derakhshan
hossein derakhshan

Reputation: 771

why do i have to reload the app after change direction from LTR to RTL in react-native?

I use I18nManager.forceRTL(true) in initialState or componentDidMount life cycle but it does't work and if I reload my App, it works.

I mean I have to reload the App to see the effect, what is the reason?

Thanks in advance

Upvotes: 3

Views: 4697

Answers (3)

user-123
user-123

Reputation: 884

Android: Inside MainApplication:

  1. import com.facebook.react.modules.i18nmanager.I18nUtil;
  2. Add these lines to onCreate():

I18nUtil.getInstance().allowRTL(this, true);

I18nUtil.getInstance().forceRTL(this, true);

iOS: Inside AppDelegate:

  1. #import <React/RCTI18nUtil.h>
  2. Add these lines to applicationDidFinishLaunchingWithOptions:

[[RCTI18nUtil sharedInstance] allowRTL:YES];

[[RCTI18nUtil sharedInstance] forceRTL:YES];

Upvotes: 10

soroush gholamzadeh
soroush gholamzadeh

Reputation: 2784

For reloading the App you can use react-native-restart

After installing the module, you can use it like here:

// Add these at the top of your component file
import RNRestart from 'react-native-restart';
import {I18nManager} from 'react-native';

// Then use it 
...
I18nManager.forceRTL(true);
RNRestart.Restart();

If you want to use it inside of componentDidMount function of your component, You need to run this code exactly once, not more:

// Inside of your component class
componentDidMount() {
    if(!I18nManager.isRTL) {
        I18nManager.forceRTL(true);
        RNRestart.Restart();
    }
}

Upvotes: 4

Ruchi Raathore
Ruchi Raathore

Reputation: 1

You can use RNRestart.Restart() after I18nManager.forceRTL(true). It will not close your app.

Upvotes: 0

Related Questions