Skip to content

Commit 8ce3ef6

Browse files
gwynethrosegrose
and
grose
authored
Fixed issue where VictoryChart would throw an unhandled exception whe… (#2536)
Co-authored-by: grose <gwyneth.rose@formidable.com>
1 parent d9ece4a commit 8ce3ef6

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

.changeset/green-parents-argue.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"victory-chart": patch
3+
"victory-core": patch
4+
---
5+
6+
Fixed issue where VictoryChart would throw an unhandled exception when passed non-element children (fixes #2391)

packages/victory-chart/src/helper-methods.tsx

+7-8
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export function getChildren(props, childComponents, calculatedProps) {
133133
const { origin, horizontal } = calculatedProps;
134134
const parentName = props.name || "chart";
135135

136-
return childComponents.map((child, index) => {
136+
return childComponents.filter(React.isValidElement).map((child, index) => {
137137
const role = child.type && child.type.role;
138138
const style = Array.isArray(child.props.style)
139139
? child.props.style
@@ -161,11 +161,10 @@ export function getChildren(props, childComponents, calculatedProps) {
161161
}
162162

163163
export const getChildComponents = (props, defaultAxes?) => {
164-
const childComponents = React.Children.toArray(props.children);
165-
let newChildComponents = [...childComponents];
164+
let childComponents = React.Children.toArray(props.children);
166165

167166
if (childComponents.length === 0) {
168-
newChildComponents.push(defaultAxes.independent, defaultAxes.dependent);
167+
childComponents.push(defaultAxes.independent, defaultAxes.dependent);
169168
} else {
170169
const axisComponents = {
171170
dependent: Axis.getAxisComponentsWithParent(childComponents, "dependent"),
@@ -179,18 +178,18 @@ export const getChildComponents = (props, defaultAxes?) => {
179178
axisComponents.dependent.length === 0 &&
180179
axisComponents.independent.length === 0
181180
) {
182-
newChildComponents = props.prependDefaultAxes
181+
childComponents = props.prependDefaultAxes
183182
? [defaultAxes.independent, defaultAxes.dependent].concat(
184-
newChildComponents,
183+
childComponents,
185184
)
186-
: newChildComponents.concat([
185+
: childComponents.concat([
187186
defaultAxes.independent,
188187
defaultAxes.dependent,
189188
]);
190189
}
191190
}
192191

193-
return newChildComponents;
192+
return childComponents;
194193
};
195194

196195
const getDomain = (props, axis, childComponents) => {

packages/victory-core/src/victory-util/helpers.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-use-before-define */
2-
import React from "react";
2+
import React, { isValidElement } from "react";
33
import { defaults, isFunction, property, pick, assign, keys } from "lodash";
44
import { CallbackArgs } from "../types/callbacks";
55
import { ValueOrAccessor } from "../types/prop-types";
@@ -301,8 +301,10 @@ export function reduceChildren<
301301
return memo;
302302
}, initialMemo);
303303
};
304-
const childNames = children.map((c, i) => i);
305-
return traverseChildren(children, childNames);
304+
305+
const validChildren = children.filter(isValidElement);
306+
const childNames = validChildren.map((c, i) => i);
307+
return traverseChildren(validChildren, childNames);
306308
}
307309

308310
/**

0 commit comments

Comments
 (0)