我需要更新 eventEmitter3 发出的某个事件的 useCallback 挂钩。现在我用当前时间更新本地状态以触发 useCallback。这行得通,但看起来很复杂。有更好的解决方案吗?
const [referencesDidChange, setReferencesDidChange] = useState(0);
useEffect(() => {
const referencesChange = () => {
setReferencesDidChange(new Date().getTime());
};
eventEmitter.on(Events.ReferencesChange, referencesChange);
return () => {
eventEmitter.removeListener(Events.ReferencesChange, referencesChange);
};
}, []);
const renderLeaf = useCallback(props => <Leaf {...props} />, [referencesDidChange]);