react-native-actionsheet icon indicating copy to clipboard operation
react-native-actionsheet copied to clipboard

When do you support hooks?

Open khsi12345 opened this issue 4 years ago • 10 comments

I am using a functional component, not a class component. How should I give ref to <ActionSheet>? ref = {ref => this.ActionSheet = ref} ?? This method cannot be used in hooks

khsi12345 avatar May 25 '20 05:05 khsi12345

useRef sir

huuthinh245 avatar Jun 01 '20 02:06 huuthinh245

@huuthinh245 please explain how can we use useRef?

MahmudHasanMenon avatar Sep 03 '20 20:09 MahmudHasanMenon

@MahmudHasanMenon Using the example in the README as base, you can use in functional components like this:

import ActionSheet from 'react-native-actionsheet'

const Demo = (props) => {
  const refActionSheet = useRef(null);

  showActionSheet = () => {
    if (refActionSheet.current) {
      refActionSheet.current.show();
    }
  }
  
  return (
      <View>
        <Text onPress={this.showActionSheet}>Open ActionSheet</Text>
        <ActionSheet
          ref={refActionSheet}
          title={'Which one do you like ?'}
          options={['Apple', 'Banana', 'cancel']}
          cancelButtonIndex={2}
          destructiveButtonIndex={1}
          onPress={(index) => { /* do something */ }}
        />
      </View>
    )
}

allanmaral avatar Sep 14 '20 21:09 allanmaral

you save my day @allanmaral

phanvinh876 avatar Sep 23 '20 10:09 phanvinh876

Thanks to you @allanmaral !!

Esoriak avatar Feb 12 '21 01:02 Esoriak

the onPress callback is referencing the old state... is there any workaround?

noumantahir avatar Oct 01 '21 17:10 noumantahir

Thank you @allanmaral <3

trinhnguyen99 avatar Feb 26 '22 04:02 trinhnguyen99

Property 'show' does not exist on type 'never'.ts(2339)

import React, { useRef } from "react";
import { View, Text } from "react-native";
import ActionSheet from "react-native-actionsheet";

interface CrossActionSheetProps {
onCloseButtonPress?: () => void;
onButtonPress?: (index: any) => void;
arrOptions: any;
distructiveIndex: any;
headerTitle: any;
isOpenActionSheet: any;
}

// const options = [
//   "Cancel",
//   "Apple",
//   <Text style={{ color: "yellow" }}>Banana</Text>,
//   "Watermelon",
//   <Text style={{ color: "red" }}>Durian</Text>,
// ];

const CrossActionSheet = ({
headerTitle,
arrOptions,
onButtonPress,
distructiveIndex,
}: CrossActionSheetProps) => {
const refActionSheet = useRef(null);
const showActionSheet = () => {
 if (refActionSheet.current) {
   refActionSheet.current.show();
 }
};

console.log("atSheet");
return (
 <View>
   <Text onPress={showActionSheet}>Open ActionSheet</Text>
   <ActionSheet
     ref={refActionSheet}
     title={
       <Text style={{ color: "#000", fontSize: 18 }}>{headerTitle}</Text>
     }
     options={arrOptions}
     cancelButtonIndex={0}
     destructiveButtonIndex={distructiveIndex}
     onPress={(index: any) => {
       onButtonPress;
     }}
   />
 </View>
);
};
export default CrossActionSheet;

badalpub1991 avatar Jun 28 '22 18:06 badalpub1991

I am getting error const refActionSheet = useRef(null); in code(undefined is not a function) by @allanmaral

import React from 'react';
import {View, Text, useRef, useEffect} from 'react-native';
import {ActionSheetCustom as ActionSheet} from 'react-native-actionsheet';

const InvalidVersionAlert = props => {
  const refActionSheet = useRef(null);

  const showActionSheet = () => {
    if (refActionSheet.current) {
      refActionSheet.current.show();
    }
  };

  return (
    <View>
      <Text onPress={showActionSheet()}>"Open ActionSheet"</Text>
      <ActionSheet
        ref={refActionSheet}
        title={'Which one do you like ?'}
        options={['Apple', 'Banana', 'cancel']}
        cancelButtonIndex={2}
        destructiveButtonIndex={1}
        onPress={index => {
          /* do something */
        }}
      />
    </View>
  );
};

export default InvalidVersionAlert;

badalWiser avatar Jan 13 '23 14:01 badalWiser

@badalWiser you are invoking the showActionSheet function as you pass it as a prop.

The updated sample code using TypeScript for future reference. Remember to install @types/react-native-actionsheet

import { Text, View } from 'react-native';
import { useRef } from 'react';
import ActionSheet from 'react-native-actionsheet'

const Demo: React.FC = (props) => {
  const refActionSheet = useRef<ActionSheet>(null);

  const showActionSheet = () => {
    if (refActionSheet.current) {
      refActionSheet.current.show();
    }
  }
  
  return (
      <View>
        <Text onPress={showActionSheet}>Open ActionSheet</Text>
        <ActionSheet
          ref={refActionSheet}
          title={'Which one do you like ?'}
          options={['Apple', 'Banana', 'cancel']}
          cancelButtonIndex={2}
          destructiveButtonIndex={1}
          onPress={(index) => { /* do something */ }}
        />
      </View>
    )
}

allanmaral avatar Jan 14 '23 13:01 allanmaral