4

I'm very new to React so please forgive me if this is a stupid question but I'm stuck on the following:

Currently I have this:

export default connect()(PrechatForm);

Now I want to translate some text of the text inside the render function with react-i18next. Their guides say I have to do something like this:

export default translate()(PrechatForm);

But since the connect function is already there I'm not sure how to combine these to. In the end I guess it should look something like this: (which isn't valid JS of course)

export default connect(PrechatForm)()translate()(PrechatForm);

The whole example looks like this:

import { connect } from 'react-redux'
import { translate } from 'react-i18next';

class PrechatForm extends Component {
  constructor(props) {
    super(props);
  }
  render() {
  const { t } = this.props;
    return (
      {t.('translateme')}
    );
  }
}

export default connect()(PrechatForm);

1 Answer 1

9

Straight forward solution is to use

export default translate()(connect()(PrechatForm));

I.e. first connecting to Redux and then translating the connected component.

EDIT: To help visualise you can see it like this

const connector = connect();
const translator = translate();

export default translator(connector(PrechatForm));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the edit, that makes it clear whats happening!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.