我正试图在类内调用dispatch。所有可能的方法都会把我引向死胡同。也许我的情况有点特殊:
我有一个要重写的类,在重写的函数中,我确实需要调用dispatch。
class RolloverModifierEx extends RolloverModifier {
override hitTestRenderableSeries(rs: IRenderableSeries, mousePoint: Point): HitTestInfo {
const hitTest = rs.hitTestProvider.hitTestXSlice(mousePoint.x, mousePoint.y, this.hitTestRadius);
// some code ...
this.dispatch(updateCurrentValue({
title: dataSeriesName ,
currentValue: yValueCalculated)
}));
// some more code ...
return hitTest;
}
}
我尝试过不同的方式,比如使用";"连接";从react redux,或者为调度创建一个包装类,但所有这些都让我遇到了同样的问题,即从react组件外部调用调度是不可能的。
我有点无知,真的需要帮助。。。
找到问题的解决方案:
我使用构造函数将存储传递到类中,然后就可以对此调用dispatch。感谢@Javokhir对商店的提示。
class RolloverModifierEx extends RolloverModifier {
private store : any;
constructor(props){
super(props);
this.store = props.store;
}
override hitTestRenderableSeries(rs: IRenderableSeries, mousePoint: Point): HitTestInfo {
const hitTest = mousePoint && rs.hitTestProvider.hitTestXSlice(mousePoint.x, mousePoint.y, this.hitTestRadius);
// some code... than finally call dispatch
this.store.dispatch(updateCurrentValue(
{
title: hitTest.dataSeriesName,
currentValue: newYValue
}));
}
return hitTest;
}
}
从外部来看,这被称为:
import { useStore } from 'react-redux';
.....
const store: any = useStore();
....
const rolloverModifier = new RolloverModifierEx({
id: 'rollover',
snapToDataPoint: false,
store
});