Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When do you support hooks? #112

Open
khsi12345 opened this issue May 25, 2020 · 10 comments
Open

When do you support hooks? #112

khsi12345 opened this issue May 25, 2020 · 10 comments

Comments

@khsi12345
Copy link

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

@huuthinh245
Copy link

useRef sir

@MahmudHasanMenon
Copy link

@huuthinh245 please explain how can we use useRef?

@allanmaral
Copy link

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

@phanvinh876
Copy link

you save my day @allanmaral

@Esoriak
Copy link

Esoriak commented Feb 12, 2021

Thanks to you @allanmaral !!

@noumantahir
Copy link

noumantahir commented Oct 1, 2021

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

@trinhnguyen99
Copy link

Thank you @allanmaral <3

@badalpub1991
Copy link

badalpub1991 commented Jun 28, 2022

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;

@badalWiser
Copy link

badalWiser commented Jan 13, 2023

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;

@allanmaral
Copy link

allanmaral commented Jan 14, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants