Skip to content

Remove props.key workaround #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ratson opened this issue May 31, 2016 · 5 comments
Closed

Remove props.key workaround #7

ratson opened this issue May 31, 2016 · 5 comments

Comments

@ratson
Copy link
Owner

ratson commented May 31, 2016

Pull request #6 introduced a workaround for facebook/react#2517.
Should be removed when React fixed it.

@gerhardsletten
Copy link

I am also experience trouble with this. If I connect a component to state.intl.locale the componentWillReceiveProps function is not called when locale is changed in store.

componentWillReceiveProps (nextProps) {
  if (this.props.intLang !== nextProps.intLang) {
    console.log('page locale', this.props.intLang, ' => ', nextProps.intLang)
  }
}

But if I remove the key in mapStateToProps in IntlProvider it works as expected.

const mapStateToProps = (state, {intlSelector = defaultSelector}) => {
  const intl = intlSelector(state)
  return {
    ...intl
  }
}

@ratson
Copy link
Owner Author

ratson commented Oct 8, 2017

@gerhardsletten From my test, I don't experience this problem, so I am planning to remove this hack and use the intlSelector directly, so anyone need this could provide their own intlSelector. What do you think?

@gerhardsletten
Copy link

Sorry, been some time since I touched this, but in this use-case we also kept an preference-reducer, so we connected our views to it to re-render / load localized content in the newly chosen language. It was a stack with many players, so it could be some other dependency which made the componentWillReceiveProps check not work.

@ratson
Copy link
Owner Author

ratson commented Oct 25, 2017

A custom intlSelector with key property is equivalent to this hack, so people need this could provide their implementation.

@ratson ratson closed this as completed Oct 25, 2017
@kimmoahola
Copy link

Hi @ratson, removing the key broke our app.

Is this a good workaround (or is there a simpler one?) to the problem where changing locale does not re-render components?

import { connect } from 'react-redux';
import { IntlProvider } from 'react-intl';

function defaultSelector(state) {
  return {
    ...state.intl,
    key: state.intl.locale,
  };
}

const mapStateToProps = (state, { intlSelector = defaultSelector }) => intlSelector(state);

export default connect(mapStateToProps)(IntlProvider);

And then use it like:

import { Provider } from 'react-redux';
import CustomIntlProvider from './i18n/customIntlProvider';
import RoutedApp from './RoutedApp';

....

  render() {
    return (
      <Provider store={store}>
        <CustomIntlProvider>
          <RoutedApp />
        </CustomIntlProvider>
      </Provider>
    );
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants