React
import { useEffect } from 'react';
// 组件
const Index = () => {
// 定义方法
const testFunction = (arg) => {
alert(`调用成功${arg}`);
}
useEffect(() => {
// 在useEffect中暴露方法到window上。
// ts 中可以这样
(window as any).test = testFunction;
// js 直接暴露既可
// window.test = testFunction;
},[])
return <div>测试安卓调用reactjs方法</div>
}
Android
String arg = "测试调用reactjs方法";
webView.loadUrl("javascript:test('"+arg+"')");