Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use react-native-action-sheet with hooks

I'm trying to use https://github.com/expo/react-native-action-sheet in a functional component using the provided hook useActionSheet(). I'm already using the class components version without any problem but I'd like to switch to functional.

React version is 16.9.0

This is my component

import {
  connectActionSheet,
  useActionSheet,
} from "@expo/react-native-action-sheet";
import React, { Component } from "react";
import { View, Text, SafeAreaView } from "react-native";
import TaButton from "../components/TaButton";
import { typography, styles, buttons } from "../components/Styles";

const UploadUI: React.FC<{
  description: string;
  label: string;
}> = (props) => {
  const { showActionSheetWithOptions } = useActionSheet();

  const openActionSheet = () => {
    console.log("TEST - Choosing action now");
    const options = [
      "Scegli dalla libreria",
      "Scatta una foto",
      "Carica un file",
      "Annulla",
    ];
    //const destructiveButtonIndex = 0;
    const cancelButtonIndex = options.length - 1;

    showActionSheetWithOptions(
      {
        options,
        cancelButtonIndex,
        //destructiveButtonIndex,
      },
      (buttonIndex) => {
        // Do something here depending on the button index selected
        switch (buttonIndex) {
          case 0:
            console.log('Case 0')
            
            return;
          case 1:
            console.log("Case 1");
            return;
          case 2:
            console.log("Case 2");
            return;

          default:
        }
      }
    );
  };

  const { description, label } = props;
  return (
    <View style={{ flexDirection: "row", height: 50, marginBottom: 30 }}>
      <View style={{ flex: 0.7, justifyContent: "center" }}>
        <Text style={typography.body}>{description}</Text>
      </View>
      <View style={{ flex: 0.3 }}>
        <TaButton
          style={buttons.primary}
          labelStyle={buttons.primaryLabel}
          onPress={() => openActionSheet()}
          label={label}
        />
      </View>
    </View>
  );
};

const URWStep4: React.FC = (props) => {
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <View style={{ paddingVertical: 30, marginBottom: 20 }}>
          <Text style={typography.title}>
            Ci serviranno alcuni documenti per verificare la tua identità
          </Text>
        </View>
        <UploadUI
          description="Carta d'identità - Fronte"
          label="Carica"
        ></UploadUI>
        <UploadUI
          description="Carta d'identità - Retro"
          label="Carica"
        ></UploadUI>
      </View>
    </SafeAreaView>
  );
};

export default connectActionSheet(URWStep4);

When clicking on buttons, the sentence "TEST - Choosing action now" is logged ad expected, but nothing more happens. The actionsheet does not open.

like image 367
zeights Avatar asked Aug 11 '20 13:08

zeights


People also ask

What is an action sheet in React Native?

An action sheet is a UI element that renders a context menu on the bottom of the screen. They are often optimized for various screen sizes and have robust support for icons. Here is a simple example: In this article, we will build and customize an iOS-like action sheet in React Native using the react-native-action-sheet module.

What are the best tools for monitoring react native apps?

Thank you so much for reading! LogRocket: Instantly recreate issues in your React Native apps. LogRocket is a React Native monitoring solution that helps you reproduce issues instantly, prioritize bugs, and understand performance in your React Native apps.

How to run react native application in Visual Studio Code?

To start the application run npx react-native run-ios inside your React Native project folder. Open your project in VS Code and head to the file named App.js.And remove the code under the return statement and replace it with:

How to display an iOS action sheet with destructive buttons?

Display an iOS action sheet. The options object must contain one or more of: destructiveButtonIndex (int or array of ints) - indices of destructive buttons in options userInterfaceStyle (string) - the interface style used for the action sheet, can be set to light or dark, otherwise the default system style will be used


Video Answer


1 Answers

Check you have wrapped your top-level component with <ActionSheetProvider /> even when using hooks

https://github.com/expo/react-native-action-sheet#1-wrap-your-top-level-component-with-actionsheetprovider-

like image 177
Simoncar Avatar answered Oct 20 '22 04:10

Simoncar