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

@mozart25/native-react-bridge

v1.0.4

Published

A package for communicating between native code and React web views

Downloads

275

Readme

@mozart25/native-react-bridge

This package provides a NativeBridge class and useNativeBridge hook for communicating between native code and React web views.

Installation

npm install @mozart25/native-react-bridge

Usage(사용법)

Using NativeBridge class

import { NativeBridge } from "@mozart25/native-react-bridge";

const bridge = new NativeBridge();

// Send data to native
bridge.sendDataToNative({ message: "Hello from React!" });

Using useNativeBridge hook

import { useNativeBridge } from "@mozart25/native-react-bridge";

const MyComponent = () => {
  const { nativeData, sendDataToNative } = useNativeBridge();

  return (
    <div>
      <button onClick={() => sendDataToNative({ message: "Hello Native!" })}>
        Send Data to Native
      </button>
      {nativeData && <p>Received from native: {JSON.stringify(nativeData)}</p>}
    </div>
  );
};

ios Swift

import WebKit

class WebViewController: UIViewController, WKScriptMessageHandler {
    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // WKWebView 설정
        let contentController = WKUserContentController()
        contentController.add(self, name: "iosHandler")  // 메시지 핸들러 추가

        let config = WKWebViewConfiguration()
        config.userContentController = contentController

        webView = WKWebView(frame: self.view.bounds, configuration: config)
        view.addSubview(webView)

        // 웹페이지 로드
        if let url = URL(string: "https://your-react-webview-url.com") {
            webView.load(URLRequest(url: url))
        }
    }

    // JavaScript에서 수신한 메시지 처리
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "iosHandler" {
            print("Message from JS: \(message.body)")
            // JS에서 보낸 메시지를 처리하는 로직
        }
    }
}

android Kotlin

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    private lateinit var webView: WebView

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView = findViewById(R.id.webView)

        // JavaScript 설정 활성화
        webView.settings.javaScriptEnabled = true

        // Android 브릿지 추가 (JavascriptInterface)
        webView.addJavascriptInterface(WebAppInterface(this), "AndroidBridge")

        // 웹 페이지 로드
        webView.loadUrl("https://your-react-webview-url.com")

        // WebView 클라이언트 설정
        webView.webViewClient = WebViewClient()
    }

    // 네이티브에서 웹으로 데이터 전송
    fun sendDataToWeb(data: String) {
        webView.post {
            webView.evaluateJavascript("window.receiveDataFromNative('$data')", null)
        }
    }

    // JavascriptInterface 클래스
    class WebAppInterface(private val activity: MainActivity) {

        @JavascriptInterface
        fun receiveMessage(message: String) {
            // React에서 받은 데이터를 처리하는 로직
            println("Message from React: $message")
            // 네이티브에서 웹으로 데이터 보내기
            activity.sendDataToWeb("{\"message\": \"Hello from Android Native!\"}")
        }
    }
}

License

MIT