Skip to content

Commit

Permalink
feat(taro): 增加 useScope hook 以访问小程序原生作用域,close #4600
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Oct 14, 2019
1 parent d67490a commit 67dfbf7
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/taro-alipay/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -76,6 +77,7 @@ export const Taro = {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-jd/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -75,6 +76,7 @@ export const Taro = {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-qq/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -73,6 +74,7 @@ export const Taro = {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useImperativeHandle,
useContext,
createContext,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-quickapp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
useReachBottom,
usePageScroll,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -66,6 +67,7 @@ export const Taro = {
useReachBottom,
usePageScroll,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-swan/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -75,6 +76,7 @@ export const Taro = {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-tt/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -75,6 +76,7 @@ export const Taro = {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-weapp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -77,6 +78,7 @@ export const Taro = {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
9 changes: 9 additions & 0 deletions packages/taro/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export function useRouter () {
return hook.router
}

export function useScope () {
const hook = getHooks(Current.index++)
if (!hook.scope) {
hook.component = Current.current
hook.scope = hook.component.$scope
}
return hook.scope
}

export function useReducer (
reducer,
initialState,
Expand Down
3 changes: 3 additions & 0 deletions packages/taro/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
forceUpdateCallback as internal_force_update
} from './hooks'
import { Current } from './current'
Expand Down Expand Up @@ -89,6 +90,7 @@ export {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down Expand Up @@ -135,6 +137,7 @@ export default {
useShareAppMessage,
useTabItemTap,
useRouter,
useScope,
useRef,
useCallback,
useMemo,
Expand Down
5 changes: 5 additions & 0 deletions packages/taro/types/taro.hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,9 @@ declare namespace Taro {
* 获取页面传入路由相关参数
*/
function useRouter (): RouterInfo
/**
* 获取小程序原生作用域
* 等同于类组件时 this.$scope
*/
function useScope (): any
}

0 comments on commit 67dfbf7

Please sign in to comment.