我收到错误TypeError:在我的代码上,可能无法在严格模式函数或调用的参数对象上访问“调用者”、“被调用者”和“参数”属性
。我看过this answer , this answer , this answer , this answer , this answer ,和this answer .
我明白
The 5th edition of ECMAScript (ES5) forbids use of arguments.callee() in strict mode. Avoid using arguments.callee() by either giving function expressions a name or use a function declaration where a function must call itself.
所以我的问题是如何修改我的代码来绕过这个问题,因为我不明白通过给函数表达式命名或使用函数必须调用自身的函数声明
意味着什么就实际解决方案而言......
我的钩子(Hook)
export default (initialState, durationInMs = 200, options = {}) => {
const [internalState, setInternalState] = useState(initialState);
const debouncedSetter = useDebouncedCallback(
setInternalState,
durationInMs,
options
);
return [internalState, debouncedSetter];
};
我如何在代码中调用它
const [searchText, setSearchText] = useDebouncedState("null", 200, {
maxWait: 1000,
});
抛出错误的地方
<input
onChange={(e) => setSearchText.callback(e.target.value)}
/>
使用DebouncedCallback函数
export interface CallOptions {
leading?: boolean;
trailing?: boolean;
}
export interface Options extends CallOptions {
maxWait?: number;
}
export interface ControlFunctions {
cancel: () => void;
flush: () => void;
pending: () => boolean;
}
export default function useDebouncedCallback<T extends (...args: any[]) => ReturnType<T>>(func: T, wait?: number, options?: Options): DebouncedState<T>;
这是根据控制台导致代码出现问题的
var debounced = react_1.useCallback(function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var time = Date.now();
var isInvoking = shouldInvoke(time);
lastArgs.current = args;
lastThis.current = _this;
lastCallTime.current = time;
if (isInvoking) {
if (!timerId.current && mounted.current) {
// Reset any `maxWait` timer.
lastInvokeTime.current = lastCallTime.current;
// Start the timer for the trailing edge.
startTimer(timerExpired, wait);
// Invoke the leading edge.
return leading ? invokeFunc(lastCallTime.current) : result.current;
}
if (maxing) {
// Handle invocations in a tight loop.
startTimer(timerExpired, wait);
return invokeFunc(lastCallTime.current);
}
}
if (!timerId.current) {
startTimer(timerExpired, wait);
}
return result.current;
}, [invokeFunc, leading, maxing, shouldInvoke, startTimer, timerExpired, wait]);
var pending = react_1.useCallback(function () {
return !!timerId.current;
}, []);
var debouncedState = react_1.useMemo(function () { return ({
callback: debounced,
cancel: cancel,
flush: flush,
pending: pending,
}); }, [debounced, cancel, flush, pending]);
return debouncedState;
}
exports.default =
最佳答案
问题是我的产品服务器上有过时的代码,所以我正在查看的代码要新得多。在部署的代码中,我的 setSearchText
的 usestate Hook 缺少 .callback
,并以某种方式触发了错误的错误
关于javascript - TypeError : 'caller' , 'callee' 和 'arguments' 属性可能无法在严格模式函数或调用的参数对象上访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66678481/