简体   繁体   English

react-native-dropdown-picker onChange 处理程序

[英]react-native-dropdown-picker onChange handler

I am developing an app in React Native Expo which is supposed to work both on mobile devices and on the web.我正在 React Native Expo 中开发一个应用程序,它应该可以在移动设备和网络上运行。 I have a problem with abovementioned library我对上述图书馆有疑问

How should I trigger an action on change of selection in react-native-dropdown-picker?我应该如何在 react-native-dropdown-picker 中触发对选择更改的操作? There are only props like只有像这样的道具

  open,
  setOpen,
  value,
  setValue,
  items,
  setItems,

open/setOpen is a boolean only to track open state, items/setItems is an array of objects of expected by dropdown shape, so only setValue seem to be the one that can do the job. open/setOpen 是一个布尔值,仅用于跟踪打开状态,items/setItems 是下拉列表形状预期的对象数组,因此似乎只有 setValue 可以完成这项工作。 Value however is a function with always empty name field and I don't know how and if I can use it to handle any actions, only useEffect react to the change of value state variable.然而,值是一个名称字段始终为空的函数,我不知道如何以及是否可以使用它来处理任何操作,只有 useEffect 对值状态变量的变化做出反应。 The thing is that it should not be happening in useEffect I think, but in event handler, and I have issues on mobile devices (both on web and in the mobile app).问题是我认为它不应该发生在 useEffect 中,而是发生在事件处理程序中,而且我在移动设备(网络和移动应用程序)上都有问题。 This compoennt is for React Native, but strangely it works on dektops like a charm and not in mobile apps and on mobile devices.该组件适用于 React Native,但奇怪的是,它可以像魅力一样在桌面上运行,而不是在移动应用程序和移动设备上运行。 Clearly I am doing something wrong, I just don't know what.显然我做错了什么,我只是不知道是什么。 This is the component code这是组件代码

import React from "react";
import { Platform, StyleSheet, View } from "react-native";
import DropDownPicker from "react-native-dropdown-picker";
import { FONTS, COLORS } from "../../constants/ui";

interface Props {
  open: boolean;
  value: string;
  items: any[];
  setOpen: React.Dispatch<any>;
  setValue: React.Dispatch<any>;
  setItems: React.Dispatch<any>;
}

export const PlatformDropdown: React.FC<Props> = ({
  open,
  setOpen,
  value,
  setValue,
  items,
  setItems,
}) => {
  return Platform.OS === "android" ? (
    <View style={[open && styles.dropdownCanvas]}>
      <DropDownPicker
        open={open}
        value={value}
        items={items}
        setOpen={setOpen}
        setValue={setValue}
        setItems={setItems}
        placeholder={`Rating`}
        showTickIcon={false}
        style={styles.dropdown}
        containerStyle={styles.dropdownContainerStyle}
        placeholderStyle={styles.dropdownPlaceholderStyle}
        dropDownContainerStyle={styles.dropDownContainerStyle}
        listItemLabelStyle={styles.dropdownListItemLabelStyle}
        selectedItemContainerStyle={styles.dropdownSelectedItemContainerStyle}
        zIndex={30}
      />
    </View>
  ) : (
    <DropDownPicker
      open={open}
      value={value}
      items={items}
      setOpen={setOpen}
      setValue={setValue}
      setItems={setItems}
      placeholder={`Rating`}
      showTickIcon={false}
      style={styles.dropdown}
      containerStyle={styles.dropdownContainerStyle}
      placeholderStyle={styles.dropdownPlaceholderStyle}
      dropDownContainerStyle={styles.dropDownContainerStyle}
      listItemLabelStyle={styles.dropdownListItemLabelStyle}
      selectedItemContainerStyle={styles.dropdownSelectedItemContainerStyle}
      zIndex={30}
    />
  );
};

const styles = StyleSheet.create({
  dropdown: {
    flexDirection: "row",
    padding: 10,
    maxWidth: "100%",
    backgroundColor: COLORS.greys["gray-300"],
    borderRadius: 7,
    marginVertical: 7,
  },
  dropdownContainerStyle: { borderColor: COLORS.greys["gray-400"] },
  dropdownPlaceholderStyle: {
    color: COLORS.FONT_COLOR,
    fontWeight: "bold",
  },
  dropDownContainerStyle: {
    backgroundColor: COLORS.greys["gray-100"],
    padding: 5,
    borderColor: COLORS.greys["gray-400"],
  },
  dropdownListItemLabelStyle: {
    color: COLORS.FONT_COLOR,
    padding: 10,
    fontFamily: FONTS.text,
  },
  dropdownSelectedItemContainerStyle: {
    backgroundColor: COLORS.greys["gray-300"],
    borderRadius: 3,
  },
  dropdownCanvas: { minHeight: 190 },
});

and this is useEffect responsible for managing actions on dropdown selection这是 useEffect 负责管理下拉选择的操作

  useEffect(() => {
    setLabs([]);
    switch (value) {
      case "rating":
        sortBy("rating", "desc");
        break;
      case "price":
        console.log(labs);
        const byPrice = labs.sort((a, b) => {
          const testIdx = a.tests.findIndex(({ title }) => title === testName);
          return a.tests[testIdx].price - b.tests[testIdx].price;
        });
        sortTimeoutRef = setTimeout(() => setLabs([...byPrice]), 200);
        break;
      case "distance":
        sortByDistance();
        break;
      default:
        sortBy("rating", "desc");
        break;
    }
    return () => {
      clearTimeout(sortTimeoutRef);
    };
  }, [value]);

Thanks a lot for any advice.非常感谢您的任何建议。

There are more props than you have listed.道具比您列出的更多。

The prop that provides what you need, is probably onChangeValue .提供您所需的道具可能是onChangeValue

For the complete list of props, that are available, you can check out the documentation有关可用道具的完整列表,您可以查看文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native 如何以编程方式打开 react-native-dropdown-picker - React Native how to open react-native-dropdown-picker programmatically react-native-dropdown-picker 按条件更改下拉背景颜色 - react-native-dropdown-picker changing dropdown background color by condition 无法从 react-native-dropdown-picker 中选择项目 - unable to select item from react-native-dropdown-picker react-native-dropdown-picker 列出的项目未正确显示(覆盖) - react-native-dropdown-picker listed items not showing properly (Overlay) 如何在 react-native-dropdown-picker 中调整模型大小 - How to resize Model in react-native-dropdown-picker react-native-dropdown-picker,如何修复下拉选择器覆盖在其他组件上 - react-native-dropdown-picker, how to fix the dropdown picker overlay on other component 有没有办法将下拉选择器中每个条目的 numberOfLines 设置为 1? (react-native-dropdown-picker) - Is there a way to set numberOfLines to 1 of each entry in the dropdown picker? (react-native-dropdown-picker) 有没有办法降低下拉选择器的高度(react-native-dropdown-picker 模块) - Is there a way to decrease the height of dropdown picker (react-native-dropdown-picker module) react-native-dropdown-picker,如何从项目中获取选定的索引 - react-native-dropdown-picker, how to get selected index from Items react-native-dropdown-picker: TypeError: undefined is not an object (evalating &#39;this.setState&#39;) - react-native-dropdown-picker: TypeError: undefined is not an object (evaluating 'this.setState')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM