【发布时间】:2017-03-15 18:51:17
【问题描述】:
我正在使用 Victory JS 在 React 中创建图表。 Victory 有一个名为 <VictoryBar> 的组件,它可以使用一个名为 labelComponent 的道具,我正在传递 <BarLabel/>。
例子:
<VictoryBar
labelComponent={<BarLabel/>}
/>
问题是我使用 Flow 进行类型检查,VictoryBar 将 props 添加到 <BarLabel/>。这是我的 BarLabel 道具和组件本身的类型。我在评论中解释了问题。
type BarLabelProps = {
style?: Object,
x?: number,
y0?: number,
// The problem lies here. When datum is optional I get the flow error
// that props.datum.name is invalid because .name cannot be accessed from a
// possibly null value. When it is required (no question mark) I get an error
// because when I pass the component to <VictoryBar /> it does not contain
// the prop datum until VictoryBar does it's thing.
datum?: Object,
};
function BarLabel(props: BarLabelProps) {
return (
<text
textAnchor="middle"
style={props.style}
x={props.x + 70}
y={props.y0 + 15}
>
<tspan style={props.style}>
{props.datum.name}
</tspan>
</text>
);
}
流程文档提供了一个在高阶函数上指定道具的示例,但对我的场景没有帮助。 如何告诉 flow 这些道具是通过VictoryBar 添加的?
【问题讨论】:
-
这是一个有趣的问题。我对 React 和 Flow 的特殊情况不是很有经验,但我认为目前没有办法对此进行建模。
-
@Pizza-r0b,你能想为此抛出一个 repro-repo/codesandbox 吗?我想帮忙,但我有点懒,不想设置整个事情