Skip to content

Commit

Permalink
fix(react-intl): fix children type of FormattedMessage, fix #3117
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Aug 16, 2021
1 parent bf0f7a0 commit 567a131
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
8 changes: 2 additions & 6 deletions packages/react-intl/src/components/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Props<
> extends MessageDescriptor {
values?: V
tagName?: React.ElementType<any>
children?(...nodes: React.ReactNodeArray): React.ReactElement | null
children?(nodes: React.ReactNodeArray): React.ReactElement | null
ignoreTag?: IntlMessageFormatOptions['ignoreTag']
}

Expand Down Expand Up @@ -47,12 +47,8 @@ function FormattedMessage(props: Props) {
ignoreTag,
})

if (!Array.isArray(nodes)) {
nodes = [nodes]
}

if (typeof children === 'function') {
return children(nodes)
return children(Array.isArray(nodes) ? nodes : [nodes])
}

if (Component) {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-intl/tests/unit/testUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function mountFormattedComponentWithProvider<P>(
Comp: React.ComponentType<P>
) {
return (
props: P & {children?(...nodes: React.ReactNodeArray): React.ReactNode},
props: P & {children?(nodes: React.ReactNodeArray): React.ReactNode},
providerProps: IntlConfig = {locale: 'en'}
) => {
const result = render(
Expand All @@ -22,7 +22,7 @@ export function mountFormattedComponentWithProvider<P>(
const {rerender} = result
const rerenderProps = (
newProps: P & {
children?(...nodes: React.ReactNodeArray): React.ReactNode
children?(nodes: React.ReactNodeArray): React.ReactNode
} = props,
newProviderProps: IntlConfig = providerProps
) =>
Expand Down

1 comment on commit 567a131

@SRachamim
Copy link

@SRachamim SRachamim commented on 567a131 Aug 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another breaking change in a patch version.

TS2322: Type '(label: string) => JSX.Element' is not assignable to type '(nodes: ReactNodeArray) => ReactElement<any, string | JSXElementConstructor> | null'.
Types of parameters 'label' and 'nodes' are incompatible.

It breaks TS compilation in code that used to work for years. I implicitly understand you follow Sentimental Versioning (instead of Semantic Versioning), but still I suggest the following:

  1. Declare you don't follow semantic versioning in the README.md.
  2. At least trying to batch breaking changes like this and put them on a single minor release with a [BREAKING_CHANGE] notice on the changelog.

This will enhance the DX of the consumers.

Anyway, thanks again for this change!

Please sign in to comment.