最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native declare interface as void for onPress call typescript error - Stack Overflow

programmeradmin1浏览0评论

I am trying to get rid of this typescript error from onPress call inside my TouchableOpacity ponent.

Called when the touch is released, but not if cancelled (e.g. by a scroll that steals the responder lock).

No overload matches this call.
  Overload 1 of 2, '(props: (TouchableOpacityProps & GenericTouchableProps) | Readonly<TouchableOpacityProps & GenericTouchableProps>): TouchableOpacity', gave the following error.
    Type 'void' is not assignable to type '(((event: GestureResponderEvent) => void) & (() => void)) | undefined'.
  Overload 2 of 2, '(props: TouchableOpacityProps & GenericTouchableProps, context: any): TouchableOpacity', gave the following error.
    Type 'void' is not assignable to type '(((event: GestureResponderEvent) => void) & (() => void)) | undefined'.ts(2769)
index.d.ts(5265, 5): The expected type es from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TouchableOpacity> & Pick<Readonly<TouchableOpacityProps & GenericTouchableProps> & Readonly<...>, "hitSlop" | ... 36 more ... | "containerStyle"> & Partial<...> & Partial<...>'
index.d.ts(5265, 5): The expected type es from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TouchableOpacity> & Pick<Readonly<TouchableOpacityProps & GenericTouchableProps> & Readonly<...>, "hitSlop" | ... 36 more ... | "containerStyle"> & Partial<...> & Partial<...>'

as you can see on my code below this error appers on my onPress call from my TouchableOpacity button.

  const handleChange = (id: Number) => {
    switch (id) {
      case 0:
        console.log('Se preciono el primer boton');
        break;
      default:
        console.log('default');
    }
  };

  return (
    <View style={styles.tabsContainer}>
      <TouchableOpacity style={styles.tab} onPress={handleChange(0)}>
        <Text style={styles.text}>TITULO DE PRUEBA</Text>
      </TouchableOpacity>
    </View>
  );
};

I think I just need to declare this onPress call as a void function but I can't figure how.. Does someone have any suggestion?

I am trying to get rid of this typescript error from onPress call inside my TouchableOpacity ponent.

Called when the touch is released, but not if cancelled (e.g. by a scroll that steals the responder lock).

No overload matches this call.
  Overload 1 of 2, '(props: (TouchableOpacityProps & GenericTouchableProps) | Readonly<TouchableOpacityProps & GenericTouchableProps>): TouchableOpacity', gave the following error.
    Type 'void' is not assignable to type '(((event: GestureResponderEvent) => void) & (() => void)) | undefined'.
  Overload 2 of 2, '(props: TouchableOpacityProps & GenericTouchableProps, context: any): TouchableOpacity', gave the following error.
    Type 'void' is not assignable to type '(((event: GestureResponderEvent) => void) & (() => void)) | undefined'.ts(2769)
index.d.ts(5265, 5): The expected type es from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TouchableOpacity> & Pick<Readonly<TouchableOpacityProps & GenericTouchableProps> & Readonly<...>, "hitSlop" | ... 36 more ... | "containerStyle"> & Partial<...> & Partial<...>'
index.d.ts(5265, 5): The expected type es from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<TouchableOpacity> & Pick<Readonly<TouchableOpacityProps & GenericTouchableProps> & Readonly<...>, "hitSlop" | ... 36 more ... | "containerStyle"> & Partial<...> & Partial<...>'

as you can see on my code below this error appers on my onPress call from my TouchableOpacity button.

  const handleChange = (id: Number) => {
    switch (id) {
      case 0:
        console.log('Se preciono el primer boton');
        break;
      default:
        console.log('default');
    }
  };

  return (
    <View style={styles.tabsContainer}>
      <TouchableOpacity style={styles.tab} onPress={handleChange(0)}>
        <Text style={styles.text}>TITULO DE PRUEBA</Text>
      </TouchableOpacity>
    </View>
  );
};

I think I just need to declare this onPress call as a void function but I can't figure how.. Does someone have any suggestion?

Share Improve this question edited May 13, 2021 at 17:48 BetoIGM asked May 13, 2021 at 17:39 BetoIGMBetoIGM 1771 gold badge3 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Try to wrap the calling function with wrapper function () => to avoid function execution before onPress and type error.

onPress={() => handleChange(0)}
发布评论

评论列表(0)

  1. 暂无评论