docs/api/hippy-react/customize.md
写个 React 组件,在需要渲染的地方通过 nativeName 指定到终端组件名称即可,以终端范例中的 MyView 为例:
import React from "react";
import { UIManagerModule } from "@hippy/react"
export class MyView extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.changeColor = this.changeColor.bind(this);
}
changeColor(color) {
// callUIFunction 只能接收一个实际渲染的终端节点
UIManagerModule.callUIFunction(this.instance, "changeColor", [color]);
}
render() {
return (
<div
ref={ref => this.instance = ref} // 设置 ref 方便 changeColor 获取
nativeName="MyView" // **必须:**将前端组件与终端组件进行绑定
{...this.props}
></div>
)
}
}
前端扩展模块分为三步:
注: callNative 代表前端调用客户端接口,无需返回 callNativeWithPromise 代表前端调用客户端接口,需要返回,返回一个 Promise 对象
callNtive/callNativeWithPromise 参数含义: moduleName: 代表和终端约定的模块名字,为 string 类型 functionName: 代表和终端约定的模块函数名字,为 string 类型 params: 实际携带参数, 为 Object 类型
// TestModule.js
import { callNative, callNativeWithPromise } from "@hippy/react"
/*
自定义module
*/
const TestModule = {
log(msg) {
callNative("TestModule", "log", msg)
},
helloNative(msg) {
callNative("TestModule", "helloNative", msg)
},
//这个是需要终端回调的
helloNativeWithPromise(msg) {
return callNativeWithPromise("TestModule", "helloNativeWithPromise", msg);
}
}
export { TestModule }
import React from "react";
import { Text } from "@hippy/react"
import { TestModule } from "./TestModule"
//展示自定义Module的使用
export default class TestModuleDemo extends React.Component {
static defaultProps = {};
constructor(props) {
super(props);
this.state = { hello: "TestModule log" }
//调用
TestModule.log("hello I am from js");
TestModule.helloNative({ hello: "I am from js" })
TestModule.helloNativeWithPromise({ hello: "I am from js" })
.then(rsp => this.setState({ hello: JSON.stringify(rsp) }));
}
render() {
const { hello } = this.state;
return (
<Text style={{ color: "red" }}>
{hello}
</Text>
)
}
}