ViewManager receiveCommand 已弃用?

ViewManager receiveCommand is deprecated?

我最近在 react-native 源代码中注意到以下方法:

public void receiveCommand(@NonNull T root, int commandId, @Nullable ReadableArray args)

的 ViewManager class 被标记为已弃用。因此,我尝试将其替换为未标记为已弃用的重载版本:

public void receiveCommand(@NonNull T root, String commandId, @Nullable ReadableArray args)

但是这个永远不会被调用。我想我可能还需要更改其他一些方法,但我找不到任何信息还需要做什么,没有我可以遵循的迁移指南。

有谁知道如何正确使用新的、未弃用的 receiveCommand 方法?

ViewManager 的源代码可以在这里找到: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java

如果从您的 React Native 代码中将字符串作为 dispatchViewManagerCommand 的第二个参数发送,将调用新的、未弃用的 receiveCommand 版本。不再需要覆盖 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()
    }
}

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>
        );
    }
}