簡體   English   中英

如何在 react/react-native 中獲取功能組件的節點處理程序

[英]How to get the node handler of a functional component in react/react-native

例如,在 react-native 中,如果在獲取類組件的節點處理程序時,可以執行ReactNative.findNodeHandler(this) 功能組件沒有實例,也沒有自己的引用,但是,可以使用forwardRefsuseImperativeHandle使其可引用。 所以,我正在為功能組件尋找與this似的東西。 我嘗試使用從forwardRefs獲得的引用,但它不起作用 - 它引發以下錯誤: Error: Argument appears to not be a ReactComponent. Keys: current Error: Argument appears to not be a ReactComponent. Keys: current

以下是我嘗試使用它的方式:

 export const NativeComponent = forwardRef(
  (
    { onChange }: PropsType,
    ref: ForwardedRef<BaseRef>
  ) => {
    const makeNativeCommandRequest = (name: string, params: Array<any>) => {
      UIManager.dispatchViewManagerCommand(
        findNodeHandle(ref as unknown as number),
        UIManager.getViewManagerConfig("NativeComponent").Commands[name],
        params
      );
    };
    useImperativeHandle(ref, () => ({
      command1() {
        makeNativeCommandRequest("command2", []);
      },
      command2() {
        makeNativeCommandRequest("command1", []);
      },
    }))});

考慮改用 Class 組件。 功能組件確實有實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM