You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
826 B
34 lines
826 B
/*
|
|
* create by Xumeng 2020/12/09
|
|
*/
|
|
import { useMemo } from 'react'
|
|
import { useRouter } from '@tarojs/taro'
|
|
|
|
/**
|
|
* 获取页面路由参数 hooks
|
|
*/
|
|
|
|
export function useRouterParams(key = null, transformer = null) {
|
|
const routerParams = useRouter().params || {}
|
|
// 路由参数不会动态改变
|
|
return useMemo(() => {
|
|
const params = { ...routerParams }
|
|
for (const field of Object.keys(params)) {
|
|
if (typeof params[field] === 'string') {
|
|
params[field] = decodeURIComponent(params[field])
|
|
}
|
|
}
|
|
|
|
if (!key) {
|
|
return params
|
|
}
|
|
|
|
const value = params[key]
|
|
|
|
if (typeof transformer === 'function') {
|
|
return transformer(value)
|
|
}
|
|
|
|
return value
|
|
}, [key, routerParams, transformer])
|
|
}
|
|
|