【发布时间】:2021-10-13 23:25:59
【问题描述】:
我已经定义了一个自定义容器:
const CombinedContainer = createContainer("selection", "voronoi");
const selectionStyle = {
fill: "transparent",
fillOpacity: 0.5,
stroke: "red",
strokeWidth: 1
}
type Props = {}
const ChartContainer = (props: Props) => (
<CombinedContainer
// @ts-ignore
cursorDimension="x"
selectionDimension="x"
selectionStyle={selectionStyle}
labels={({datum}) => `${datum.x}, ${datum.y.toFixed(2)} ${datum.volume}`}
/>
)
export default ChartContainer;
并像这样使用它:
<VictoryChart
width={width}
padding={padding}
minDomain={{ y: 70 }}
maxDomain={{ y: dataSet.maxDomain }}
containerComponent={<ChartContainer />}
>
...
</VictoryChart>
但这不会渲染任何东西,除非我像普通函数一样使用它:
<VictoryChart
width={width}
padding={padding}
minDomain={{ y: 70 }}
maxDomain={{ y: dataSet.maxDomain }}
containerComponent={ChartContainer({})}
>
...
</VictoryChart>
我之前定义了类似的功能组件并使用它们没有任何问题,但我看不出我在这里做错了什么。谁能帮帮我?
为什么<ChartContainer /> 不起作用,而ChartContainer() 起作用?
createContainer函数定义为:
export type ContainerType =
| "brush"
| "cursor"
| "selection"
| "voronoi"
| "zoom";
export function createContainer<V, W>(
c1: ContainerType,
c2: ContainerType
): React.ComponentType<V & W>;
这是测试它的示例项目:
https://github.com/olcayertas/victory-functional-container-componenet
【问题讨论】:
-
createContainer函数返回什么? -
它返回一个组件类型,我已经用定义更新了问题。
-
你能设置最小的可重现示例吗?
-
@AlekseyL。我已经添加了项目链接。
标签: reactjs typescript react-functional-component victory-charts