Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run function on React.Intl <FormattedMessage>

I am trying to run a function to do further string manipulation on the result of the FormattedMessage. I cannot find a way even after running it through a function as below :

<Button text={ this.testMethod(buttonText) } />

testMethod(result){
 return result;
}

In summary: I am trying to get the actual text and not the object and then do some string manipulation.

Don't want this: {id: "test", defaultMessage: "Hello there"}

Need: To access "Hello there" and for example extract the word "there"

like image 811
Afshin Ghazi Avatar asked Sep 01 '26 00:09

Afshin Ghazi


1 Answers

Yes you can. Add a component as a child of FormattedMessage to do further transformation. Here is a minimal snippet to get you started with

<FormattedMessage id="hello">
  {(text) => {
    console.log(text);
    return <>{text.toUpperCase()}</>;
  }}
</FormattedMessage>

Live Example

Edit React-intl FormattedMessage Callback

like image 159
NearHuscarl Avatar answered Sep 02 '26 14:09

NearHuscarl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!