formatjs icon indicating copy to clipboard operation
formatjs copied to clipboard

Change in FormattedMessage component behavior between versions 3.10.0 and 3.11.0

Open kplatta opened this issue 6 years ago • 7 comments

Hello, From what I can see there was a change in FormattedMessage behavior between 3.10.0 and 3.11.0 If I am right it's because of this change in this line

codesandbox.io link to reproduce https://codesandbox.io/s/bold-cdn-63krs to see a difference just change react-intl version to 3.10.0

If 'someKey' has a value 'first line {br}second line' then:

  • In 3.11.0
<FormattedMessage id="someKey" values={{br: <br/>}}>
{(...messageParts) => {
    console.log(messageParts) // [{$$typeof: Symbol(react.element), type: Symbol(react.fragment), key: null, ref: null, props: {…}, …}]
    return messageParts;
}}
</FormattedMessage>
  • In 3.10.0
<FormattedMessage id="someKey" values={{br: <br/>}}>
{(...messageParts) => {
    console.log(messageParts) // ["first line ", {$$typeof: Symbol(react.element), type: "br", key: null, ref: null, props: {…}, …}, "second line"]
    return messageParts;
}}
</FormattedMessage>

Before 3.11.0 we had message parts as function params now we have only React.Fragment element

kplatta avatar Feb 09 '20 15:02 kplatta

Yes this is to fix the missing key issue. Is this causing some issues on your part?

longlho avatar Feb 18 '20 00:02 longlho

This is a breaking change for someone that was using this API, e.g. when transforming messageParts.

rafalmaciejewski avatar Feb 18 '20 07:02 rafalmaciejewski

Hmm I don't think our API guarantees what a specific chunk is (can be string/number/ReactElement). The only guarantee I guess is that it's renderable by React (which has a specific type). It is an implementation detail that is private intentionally. Transforming specific chunk isn't necessarily linguistically safe and the order of those chunks might change. Right now we don't have a chunk type API or anything like that to identify those chunks so whatever is being done to them is pure speculation.

longlho avatar Feb 18 '20 17:02 longlho

Is it still ...chunks when now there is only one React element returned with chunks as children? Anyway, i used this feature more or less like this:

function renderChunk(chunk, index) {
    if (React.isValidElement(chunk)) {
        return React.cloneElement(chunk, {key: index});
    }
    if (typeof chunk === 'string') {
        return htmlEntities.decode(chunk);
    }
    return chunk;
}

export class FormattedMessageWrapper extends React.Component {
    render() {
        return (
            <FormattedMessage {...this.props}>
                {(...chunks) => chunks.map(renderChunk)}
            </FormattedMessage>
        );
    }
}

Could you suggest an alternative approach for achieving this without transforming messages before passing them into IntlProvider?

rafalmaciejewski avatar Feb 18 '20 18:02 rafalmaciejewski

AFAIK the number of chunks is the same (no guarantee) and they do have key now so for your use case specifically you shouldn't have to insert the key attribute in anymore.

In terms of html entities that's a separate issue and I'm not sure why you'd need to handle that separately? Do you have an example?

longlho avatar Feb 18 '20 19:02 longlho

Messages that i work with come from various sources and can contain entities like &lt;, without this transformation they are rendered as string. Workaround is to use dangerouslySetInnerHTML but it breaks when message contains also parts that are react components.

<FormattedMessage
  defaultMessage="&lt;<a>visit our website</a>&gt;"
  values={{
    a: (...chunks) => (
      <a class="external_link" target="_blank" href="https://www.shoe.com/">
        {chunks}
      </a>
    ),
  }}
>

For example above without decoding entities the visible message rendered would be &lt;visit our website&gt; instead of <visit out website>.

rafalmaciejewski avatar Feb 18 '20 19:02 rafalmaciejewski

Can you create a separate GH issue for that along w/ examples? I'm sure we've encountered your use case somewhere but need more context on it.

longlho avatar Feb 18 '20 19:02 longlho