【发布时间】:2022-01-06 11:56:49
【问题描述】:
在我的情况下, compareValue 中的 localesCollection 具有旧(未更新)值。状态不是实时的。我以为 bind 是为了拯救我的一天。
// Redux
let localesCollectionValues = useSelector((state: IStore) => state.localesStoreSlice.localesCollectionValues);
const compareValue = (fieldValue: any): void => {
console.log('compareValue', fieldValue, localesCollectionValues);
}
const attachListeners = () => {
console.log('attachListeners');
Object.keys(sdk.entry.fields).forEach((field: string) => {
const fieldRef: any = sdk.entry.fields[field];
fieldRef.locales.forEach((localeKey: string) => {
fieldRef.getForLocale(localeKey).onValueChanged(compareValue.bind(this));
});
});
};
编辑: is 来自直接在回调中使用箭头函数,问题与上述相同
编辑2: 'localesCollectionValues' 的值是代码初始化和处理程序设置时的值。之后它没有更新,它只保留 n 个旧参考。典型的内存指针/范围问题,需要一个方向来解决这个问题。
fieldRef.getForLocale(localeKey).onValueChanged(() => {
console.log(localesCollectionValues) // <- not real time,
// contains old value
});
【问题讨论】: