[英]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.