检查一个 propType 是否与另一个内部组件一起使用
Check if one propType is used with another inside Component
这里的 React 仍然很新,但我想知道是否有办法检查 组件是否将某个道具与另一个 结合使用?例如:
<Graph x={[1, 2, 3, 4]} points={10} />
让我们假设 points 属性为 Graph 组件随机分配了 10 个点,但是使用该组件的人也可以传入一个带有数值数组的 x
属性。
理论上,我想实现一个功能,在 points
属性已经被使用时使用 points
属性会在 React 中抛出错误或以某种方式警告用户。也许在这一点上像 console.log
.
这样简单的东西
您可以使用 propTypes
检查
propTypes: {
points: function(props, propName, componentName) {
if(props.x != undefined){
console.log("You cannot use points and x at the same time");
return new Error("You cannot use points and x at the same time"); // Use this if you want to throw an error.
}
}
}
这是一个例子 -> https://jsfiddle.net/69z2wepo/41180/
这里的 React 仍然很新,但我想知道是否有办法检查 组件是否将某个道具与另一个 结合使用?例如:
<Graph x={[1, 2, 3, 4]} points={10} />
让我们假设 points 属性为 Graph 组件随机分配了 10 个点,但是使用该组件的人也可以传入一个带有数值数组的 x
属性。
理论上,我想实现一个功能,在 points
属性已经被使用时使用 points
属性会在 React 中抛出错误或以某种方式警告用户。也许在这一点上像 console.log
.
您可以使用 propTypes
检查propTypes: {
points: function(props, propName, componentName) {
if(props.x != undefined){
console.log("You cannot use points and x at the same time");
return new Error("You cannot use points and x at the same time"); // Use this if you want to throw an error.
}
}
}
这是一个例子 -> https://jsfiddle.net/69z2wepo/41180/