npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-hook

v3.1.0

Published

React Native Hooks

Downloads

16

Readme

React Native Hook

Latest Version on NPM npm

如何安装

yarn add react-native-hook
第三方依赖
yarn add @react-navigation/native # >= 5.7.0
yarn add react-native-gesture-handler # >= 1.4.0
yarn add react-native-lifecycle # 生命周期
yarn add react-native-permissions # 权限

Hooks

Functions

usePermissions

检查 App 权限是否开启,未开启则可以向系统申请开启

使用
import { usePermissions } from 'react-native-hook';
import { PERMISSIONS, RESULTS } from 'react-native-permissions';

export default function Page() {
  // 权限是否打开
  // 检查 ios 端 PERMISSIONS.IOS.LOCATION_ALWAYS 权限是否打开
  const location = usePermissions([PERMISSIONS.IOS.LOCATION_ALWAYS]);

  // 权限是否不可用
  // ios 端检查 PERMISSIONS.IOS.CAMERA 权限是否不可用
  // android 端检查 PERMISSIONS.ANDROID.CAMERA 权限是否不可用
  const camera = usePermissions(
    [PERMISSIONS.IOS.CAMERA, PERMISSIONS.ANDROID.CAMERA],
    RESULTS.UNAVAILABLE,
  );

  // 权限状态
  console.log(location.state);

  // 请求权限
  const onClick = () => {
    if (location.state === false) {
      location
        .request()
        .then(() => {
          // 所有权限都已打开
        })
        .catch(({ openSettings }) => {
          openSettings();
        });
    }
  };
}

permissions

直接向系统申请未开启的权限

使用
import { permissions } from 'react-native-hook';
import { PERMISSIONS, RESULTS } from 'react-native-permissions';

export default function Page() {
  permissions
    .request(
      [PERMISSIONS.IOS.CAMERA, PERMISSIONS.ANDROID.CAMERA],
      RESULTS.GRANTED,
    )
    .then(() => {
      // 所有权限都已打开
    })
    .catch(({ openSettings }) => {
      openSettings();
    });
}

useEventEmitter

事件触发与事件监听器功能(页面创建时创建,页面销毁时销毁)

使用
import React from 'react';
import { TouchableOpacity } from 'react-native';
import { useEventEmitter } from 'react-native-hook';

// 组件
function Component() {
  // 注册事件
  useEventEmitter('Event emitter name', params => {
    console.log('Hello', params);
    // Hello World
  });

  return null;
}

// 页面
export default function App() {
  const eventEmitterName = useEventEmitter('Event emitter name');

  const onClick = () => {
    eventEmitterName.emit('World'); // 发射事件
  };

  return (
    <TouchableOpacity onPress={onClick}>
      <Component />
    </TouchableOpacity>
  );
}

usePageEventEmitter

事件触发与事件监听器功能(页面级事件,仅当前页面有效,页面创建时创建,页面销毁时销毁)

使用
import React from 'react';
import { TouchableOpacity } from 'react-native';
import { usePageEventEmitter } from 'react-native-hook';

// 组件
function Component() {
  // 注册事件
  usePageEventEmitter('Event emitter name', params => {
    console.log('Hello', params);
    // Hello World
  });

  return null;
}

// 页面
export default function Page() {
  const eventEmitterName = usePageEventEmitter('Event emitter name');

  const onClick = () => {
    eventEmitterName.emit('World'); // 发射事件
  };

  return (
    <TouchableOpacity onPress={onClick}>
      <Component />
    </TouchableOpacity>
  );
}

usePageInterval

定时器(页面级事件,页面创建时创建,页面销毁时销毁)

使用
import { usePageInterval } from 'react-native-hook';

export default function Page() {
  const pageInterval = usePageInterval(() => {
    console.log('usePageInterval');
  }, 60 * 1000);

  const onClick = () => {
    pageInterval.setEnabled(false);
  };
}

useAppActiveInterval

定时器(App 级事件,App 活跃时执行)

使用
import { useAppActiveInterval } from 'react-native-hook';

export default function App() {
  const appActiveInterval = useAppActiveInterval(() => {
    console.log('useAppActiveInterval');
  }, 60 * 1000);

  const onClick = () => {
    appActiveInterval.setEnabled(false);
  };
}

usePageGesture

设置当前页面是否支持右滑返回(页面级事件,页面创建时创建,页面销毁时销毁)

使用
import { usePageGesture } from 'react-native-hook';

export default function Page() {
  const pageGesture = usePageGesture({
    enabled: false,
  });

  const onClick = () => {
    pageGesture.setEnabled(true);
  };
}