luc*_*tom 5 react-native react-native-android
我最近在 react-native 源代码中注意到以下方法:
public void receiveCommand(@NonNull T root, int commandId, @Nullable ReadableArray args)
Run Code Online (Sandbox Code Playgroud)
ViewManager 类的标记为已弃用。因此,我尝试将其替换为未标记为已弃用的重载版本:
public void receiveCommand(@NonNull T root, String commandId, @Nullable ReadableArray args)
Run Code Online (Sandbox Code Playgroud)
但这个永远不会被调用。我想我可能还需要更改其他一些方法,但是我找不到任何信息,还需要做什么,没有可以遵循的迁移指南。
有谁知道如何正确使用新的、未弃用的 receiveCommand 方法?
ViewManager 的源代码可以在这里找到:https : //github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java
小智 8
receiveCommand如果dispatchViewManagerCommand从 React Native 代码发送字符串作为 的第二个参数,则将调用新的、未弃用的版本。没有必要再覆盖getCommandsMap()了。
例子:
CustomViewManager.kt (在 Kotlin 中,应该很容易转换为 Java)
class CustomViewManager : SimpleViewManager<CustomView>() {
...
override fun createViewInstance( context: ThemedReactContext): CustomView {
// code to instantiate your view
}
...
override fun getName(): String {
return "CustomView"
}
...
override fun receiveCommand(view: CustomView, commandId: String, args: ReadableArray?) {
when (commandId) {
"doSomething" -> doSomething()
}
}
Run Code Online (Sandbox Code Playgroud)
MyComponent.js
import { View, requireNativeComponent, UIManager, findNodeHandle } from 'react-native';
...
const CustomView = requireNativeComponent('CustomView');
...
export default class MyComponent extends Component {
...
onDoSomething = async () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this.customView),
'doSomething',
undefined,
);
};
...
render() {
return (
<View>
<CustomView
ref={(component) => {
this.customView = component;
}}
/>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
997 次 |
| 最近记录: |