Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复生命周期 hooks 里的闭包变量不更新的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Sep 2, 2019
1 parent e7dadf6 commit 3b5e039
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/remax/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import { registerLifecycle, Lifecycle, Callback } from './lifecycle';
export function useShow(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.show, callback);
}, []);
});
}

export function useHide(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.hide, callback);
}, []);
});
}

export function usePullDownRefresh(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.pullDownRefresh, callback);
}, []);
});
}

export function useReachBottom(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.reachBottom, callback);
}, []);
});
}

export function usePageScroll(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.pageScroll, callback);
}, []);
});
}

export function useShareAppMessage(
Expand All @@ -40,29 +40,29 @@ export function useShareAppMessage(
) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.shareAppMessage, callback);
}, []);
});
}

export function useTitleClick(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.titleClick, callback);
}, []);
});
}

export function useOptionMenuClick(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.optionMenuClick, callback);
}, []);
});
}

export function usePopMenuClick(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.popMenuClick, callback);
}, []);
});
}

export function usePullIntercept(callback: Callback) {
useLayoutEffect(() => {
return registerLifecycle(Lifecycle.pullIntercept, callback);
}, []);
});
}

2 comments on commit 3b5e039

@byunicorn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议把依赖的变量作为第二参数传入,而不是强制每次render都register一遍Lifecycle cc: @malash

@yesmeck
Copy link
Member Author

@yesmeck yesmeck commented on 3b5e039 Sep 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useCallback 就可以了

Please sign in to comment.