Skip to content

Commit

Permalink
improvement(update async): update useSystemInfo and useLaunchOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
innocces committed Aug 19, 2021
1 parent 4c309e2 commit 6c08d96
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/app/src/pages/useSelectorQuery/getFields/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { AtDivider, AtList, AtListItem } from 'taro-ui';
import DocPage from '@components/DocPage';
import { Video } from '@tarojs/components';
import { View } from '@tarojs/components';

import { useSelectorQuery, useEnv } from 'taro-hooks';
import { useReady, ENV_TYPE, General } from '@tarojs/taro';
Expand Down Expand Up @@ -36,7 +36,7 @@ const Query = () => {
return (
<>
<DocPage title="useSelectorQuery fields">
<Video src="" className="demo" />
<View src="" className="demo" />
<AtDivider content="属性" />
<AtList>
{rect &&
Expand Down
15 changes: 13 additions & 2 deletions packages/hooks/src/useLaunchOptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { useEffect, useState } from 'react';
import { ENV_TYPE, getLaunchOptionsSync, General } from '@tarojs/taro';
import useEnv from '../useEnv';

function useLaunchOptions(): General.LaunchOptionsApp | {} {
export type Result = General.LaunchOptionsApp | {};

function useLaunchOptions(): Result {
const env = useEnv();
return env === ENV_TYPE.WEAPP ? getLaunchOptionsSync() : {};
const [launchOptions, setLaunchOptions] = useState<Result>({});

useEffect(() => {
if (env && env === ENV_TYPE.WEAPP) {
setLaunchOptions(getLaunchOptionsSync());
}
}, [env]);

return launchOptions;
}

export default useLaunchOptions;
19 changes: 15 additions & 4 deletions packages/hooks/src/useSystemInfo/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { getSystemInfoSync } from '@tarojs/taro';
import { useEffect, useState } from 'react';
import { getSystemInfo } from '@tarojs/taro';
import { useCallback, useEffect, useState } from 'react';

export type Result = getSystemInfoSync.Result | {};
export type Result = getSystemInfo.Result | {};

function useSystemInfo(): Result {
const [systemInfo, setSystemInfo] = useState<Result>({});

useEffect(() => {
setSystemInfo(getSystemInfoSync());
getSystemInfoSync();
}, []);

const getSystemInfoSync = useCallback(() => {
try {
getSystemInfo({
success: setSystemInfo,
fail: () => setSystemInfo({}),
});
} catch (e) {
setSystemInfo({});
}
}, []);

return systemInfo;
Expand Down

2 comments on commit 6c08d96

@vercel
Copy link

@vercel vercel bot commented on 6c08d96 Aug 19, 2021

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

taro-hooks – ./

taro-hooks-innocces.vercel.app
taro-hooks-git-main-innocces.vercel.app
taro-hooks-theta.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6c08d96 Aug 19, 2021

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

taro-hooks-h5 – ./

taro-hooks-h5-git-main-innocces.vercel.app
taro-hooks-h5-innocces.vercel.app
taro-hooks-h5-green.vercel.app

Please sign in to comment.