Giter Club home page Giter Club logo

native-wechat's Introduction

A React Native library for supporting Wechat APIs on Android and iOS

Native Wechatis released under the MIT license. Current npm package version.

Motivation

Most developers using React Native to build apps are not equipped with native developing skills, not to mention connecting their apps with WeChat.

Especially, WeChat OpenSDK is something pain in the ass because of shitty docs where lots of APIs are not clear and intuitive. To help developers using React Native, I tried as much as possible to create this library. And I hope you benefit from this and be less frustrated with WeChat.

Why

  • Native WeChat is new, and many libraries like it are no longer maintained and keep up with the latest WeChat.
  • Native WeChat supports both legacy and new React Native architecture.
  • Native WeChat is a TurboModule that supports lazy loading, allowing for faster app startup.
  • Supports TypeScript for better intellisense and easy-to-check typing errors.
  • All Apis are promisified.

Getting Started

Register SDK

Before invoking any APIs, registering Native WeChat by invoking registerApp is essential.

import {registerApp} from 'native-wechat';

useEffect(()=>{
	return registerApp('wx964290141ebe9b7b');
}, [])

When invoking registerApp, there will be a listener to receive events from Wechat. registerApp returns a function to remove the listener, and no events will be received from Wechat.

Invoking API

Now is the time to invoke API. For example, we will send an auth request to Wechat and receive the code. You may use the API named sendAuthRequest here.

import {registerApp, sendAuthRequest} from 'native-wechat';
import {Button, Text} from 'react-native';
import {verifyWechatCode} from '@/api/auth/wechat'

useEffect(()=>{
	registerApp('wx964290141ebe9b7b');
}, [])

const onButtonClicked = async () =>{
  const {data: {code}} = await sendAuthRequest();
  
  await verifyWechatCode(code);
}

return <Button onClick={onButtonClicked}>
  <Text>Send Auth Request</Text>
</Button>

Most APIs in Native WeChat are promisified. So is sendAuthRequest which will return a promise. When the user confirms the request on Wechat, the promise is resolved to be data with a code. All promisifed APIs return a generic type named NativeWechatResponse.

export type NativeWechatResponse<T = Record<string, unknown>> = {
  type: string;
  errorCode: number;
  errorStr: string | null;
  data: T;
};

Support

If you have trouble using this library, do not hesitate to open an issue. I am always here to help.

native-wechat's People

Contributors

hector-chong avatar lopo12123 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.