Skip to content

Commit 85349f9

Browse files
Fix: warning each child should have a unique key (#1820)
* Fix: warning each child should have a unique key * Requested changes: use warnOnce instead of console.warn
1 parent 0c6171e commit 85349f9

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

src/TransWithoutContext.js

+55-21
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,59 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
298298
return getChildren(result[0]);
299299
};
300300

301+
const fixComponentProps = (component, index, translation) => {
302+
const componentKey = component.key || index;
303+
const comp = cloneElement(component, { key: componentKey });
304+
if (
305+
!comp.props ||
306+
!comp.props.children ||
307+
(translation.indexOf(`${index}/>`) < 0 && translation.indexOf(`${index} />`) < 0)
308+
) {
309+
return comp;
310+
}
311+
312+
function Componentized() {
313+
// <>{comp}</>
314+
return createElement(Fragment, null, comp);
315+
}
316+
// <Componentized />
317+
return createElement(Componentized);
318+
};
319+
320+
const generateArrayComponents = (components, translation) =>
321+
components.map((c, index) => fixComponentProps(c, index, translation));
322+
323+
const generateObjectComponents = (components, translation) => {
324+
const componentMap = {};
325+
326+
Object.keys(components).forEach((c) => {
327+
Object.assign(componentMap, {
328+
[c]: fixComponentProps(components[c], c, translation),
329+
});
330+
});
331+
332+
return componentMap;
333+
};
334+
335+
const generateComponents = (components, translation) => {
336+
if (!components) return null;
337+
338+
// components could be either an array or an object
339+
340+
if (Array.isArray(components)) {
341+
return generateArrayComponents(components, translation);
342+
}
343+
344+
if (isObject(components)) {
345+
return generateObjectComponents(components, translation);
346+
}
347+
348+
// if components is not an array or an object, warn the user
349+
// and return null
350+
warnOnce('<Trans /> component prop expects an object or an array');
351+
return null;
352+
};
353+
301354
export function Trans({
302355
children,
303356
count,
@@ -360,29 +413,10 @@ export function Trans({
360413
};
361414
const translation = key ? t(key, combinedTOpts) : defaultValue;
362415

363-
if (components) {
364-
Object.keys(components).forEach((c) => {
365-
const componentKey = components[c].key || c;
366-
const comp = cloneElement(components[c], { key: componentKey });
367-
if (
368-
!comp.props ||
369-
!comp.props.children ||
370-
(translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0)
371-
)
372-
return;
373-
374-
// eslint-disable-next-line react/no-unstable-nested-components
375-
function Componentized() {
376-
// <>{comp}</>
377-
return createElement(Fragment, null, comp);
378-
}
379-
// <Componentized />
380-
components[c] = createElement(Componentized);
381-
});
382-
}
416+
const generatedComponents = generateComponents(components, translation);
383417

384418
const content = renderNodes(
385-
components || children,
419+
generatedComponents || children,
386420
translation,
387421
i18n,
388422
reactI18nextOptions,

0 commit comments

Comments
 (0)