1

下午好,亲爱的朋友们。我现在对每个动作都执行了调度。我只需要在挂载页面时调度一次,在卸载页面时将其删除。告诉我如何做到这一点。

const myRef = createRef<any>();


const KeyboardAvoidingWrapper: React.FC<IKeyboardAvoidingProps> = (
  props: IKeyboardAvoidingProps,
) => {
  const dispatch = useDispatch();
  dispatch(setReference(myRef));
  if (isAndroid) {
    return (
      <ScrollView ref={myRef} style={styles.scroll} contentContainerStyle={styles.scrollContent}>
        <KeyboardAvoiding>{props.children}</KeyboardAvoiding>
      </ScrollView>
    );
  }

  return (
    <KeyboardAwareScrollView
      ref={myRef}
      style={styles.scroll}
      extraHeight={40}
      contentContainerStyle={styles.scrollContent}
    >
      {props.children}
    </KeyboardAwareScrollView>
  );
};

4

1 回答 1

0

尝试这个

// Similar to componentDidMount
useEffect(() => {
    const dispatch = useDispatch();
    dispatch(setReference(myRef));

    return () => dispatch(deleteReference(myRef)); // delete here on unmount
  }, []);
}
于 2020-10-26T08:50:55.760 回答