I currently have strings being returned from a server which have variables that need to be interpolated on the front end. I'm using React and Redux and set the strings to the component's props so that they are accessed as follows:
this.props.translations['hello-message']
An example string looks like:
Hello, ${name}!
In addition, I have the user's name being returned from the server and set to props as well. How can I interpolate the string so that the user's name appears in the string and thus in the rendered React component.
I've tried use the String.prototype.replace() method in the render of the React component as follows:
let helloMessage = this.props.translations['hello-message'].replace('${name}', 'Joe');
But this returns an error of: "Uncaught TypeError: Cannot read property 'replace' of undefined". I've tried various other strategies such as functions inside componentWillReceiveProps() or creating a helper function as mentioned here: How can I construct a Template String from a regular string? all to no avail.