3

I was wondering if it was possible to dynamically change the locale and have the app rerender without using redux.

ReactDOM.render(
<IntlProvider locale={window.locale} messages={window.messages} >
    <Router history={history}>
        <App /> 
    </Router>
</IntlProvider>,
document.getElementById('root'));
registerServiceWorker();
gouletf
  • 111
  • 1
  • 8

1 Answers1

0

You could have container component above IntlProvider and pass locale as a prop. But I imagine that would be very tedious to pass down the component tree:

class Container extends React.Component {
  state: {
    locale: window.locale
  }

  changeLocale = (locale) => this.setState({ locale })

  render() {
    return (
      <IntlProvider locale={this.state.locale} messages={window.messages} >
        <Router history={history}>
          <App changeLocale={this.changeLocale} /> 
        </Router>
      </IntlProvider>,
    )
  }
}
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
  • 1
    yes it would, it's a good idea but after some research I desided to just go ahead and use Redux. I was avoiding it because my teacher wasn't letting me but now he gave me green light to use Redux. – gouletf Apr 18 '18 at 19:22