Ad
React-intl: Change Div Direction Based On Current Locale
So, here is my App
component:
const App = () => {
const [lang, setLang] = useState("en");
return (
<IntlProvider
locale={lang}
key={lang}
messages={lang == "en" ? englishMessages : arabicMessages}
>
<AppBar>
<LangSwitch
checked={lang == "en"}
onChange={() => setLang(lang == "en" ? "ar" : "en")}
></LangSwitch>
</AppBar>
<Game />
</IntlProvider>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
and, this is my Game
component:
const Game = ()=>
(<div direction = "???">
I want to set direction to ltr if current locale is 'en' other wise to 'rtl'
</div>)
How can I read the current value of locale
-set in the parent component of IntlProvider
- inside child component of Game
and set the property direction
accordingly?
Ad
Answer
You need to pass state lang
to your Game
component,
<Game lang={lang}/>
And your Game
component should be,
const Game = (props)=> ( //provide props argument here
<div direction ={props.lang === 'en' ? 'ltr' : 'rtl'}> //check your condition
I want to set direction to ltr if current locale is 'en' other wise to 'rtl'
</div>
)
Update
Another way is wrapping your component with injectIntl
HOC.
import {injectIntl} from 'react-intl';
const Game = ({intl})=> {
console.log(intl.locale)
return (
<div direction ={intl.locale === 'en' ? 'ltr' : 'rtl'}> //check your condition
I want to set direction to ltr if current locale is 'en' other wise to 'rtl'
</div>
)
}
export default injectIntl(Game)
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad