Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use a react-intl Higher Order Component to format messages and get current locale

    15 - 16.8.4

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a component. Instead of using react-intl components (which generate markup), we’ll use the injectIntl higher order component provided by react-intl. This will provide just the string we’re looking for, make the prop simpler, and avoid creating unnecessary DOM elements.

    We’ll also use props passed to the component from the Higher Order Component to clean up some redundant code.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Let's add a place where a user can enter a review by adding a text area below the reviews. I'll just add a text area. I would also like to add a placeholder attribute to let the user know what this field is for, but it needs to be rendered in the correct language.

    I'll add the placeholder to each language in the messages file. I'll give it a key of inputplaceholder, and I'll pass the value. I'll just do this for the other languages, as well. Back in our text area, initially you may think to render the placeholder prop with a formatted message component. Let's take a look, and see what happens if we do that.

    I'll add a formatted message and I'll give it an ID prop of details.inputplaceholder. Looking in the browser, we can see our text area placeholder renders object object. The reason this happens is because the formatted message component doesn't actually render when it's inside of the HTML placeholder attribute.

    To fix this, we need to use the React-Intl inject Intl higher-order component. Let's go ahead and import that higher order component from React-Intl. Next, in the props declaration of this book detail component, I'll add an Intl prop, as this will be passed in from the Intl provider parent component.

    Now, let's go back to our text area, and we'll change that text area placeholder prop. We'll start by referencing the Intl prop and we'll call formatMessage(). That's a method, so we're going to instantiate that.

    As an argument to this method, I'll pass an object and I'll provide a key of ID and a value of detail.inputplaceholder. Using the Intl formatMessage() method is actually the underlying API that the formatted message component uses, but we bypass the React component creation step and just get the string at detail.inputplaceholder in the messages file.

    One final thing, we'll also need to wrap the book detail export in an inject Intl higher-order component. We'll add that, and to that method, we'll pass our book detail component. With those changes, our text area now renders the correct string in each language.

    One other benefit to including the Intl prop in our component declaration here is that we can get the user's locale without doing the navigator lookup in this file, because we're already doing it in the index.js file when we set up our Intl provider component.

    I'm going to go ahead and delete this locale code here and I'll update where we're searching for locale in this formatted number. We'll just grab locale off of Intl. We'll do the same here. Great. Our app hasn't changed, and we're now reducing code duplication as well.