下拉列表中的setValue不更新文本元素中的值变量。
我有一个下拉列表和setValue,可以选择下拉列表的价值。我也有一个文本元素,只要更改下拉列表,就应该显示该值,但是当我更改下拉列表时,它不会更新文本而保持空白?这是我正在使用 https://www.npmjs.com/ppackage /React-Native-Native-element-Dropdown
const App = () => {
const [products, setProducts] = useState<ProductType[] | []>([]);
const [open, setOpen] = useState(false);
const [value, setValue] = useState(' ');
const [isFocus, setIsFocus] = useState(false);
const getProducts = async () => {
try {
const response = await axios.get('http://192.168.1.32:3000/api/products');
setProducts(response.data);
} catch (error) {
// handle error
alert('no');
}
};
React.useEffect(() => {
getProducts();
}, []);
return (
<>
<AppBar
title="App"
trailing={props => (
<Button
variant="text"
title="ADD"
compact
//onPress={getProducts}
style={{marginEnd: 4}}
{...props}
/>
)}
/>
<Dropdown
style={[styles.dropdown, isFocus && {borderColor: 'blue'}]}
data={products}
search
maxHeight={300}
labelField="product"
valueField="aisle"
placeholder={!isFocus ? 'Select item' : '...'}
searchPlaceholder="Search..."
value={value}
onFocus={() => setIsFocus(true)}
onBlur={() => setIsFocus(false)}
onChange={item => {
setValue(item.value);
setIsFocus(false);
}}
/>
<TextInput label="quantity" variant="standard" />
<Text>value: {value}</Text>
</>
);
};
export default App;
const styles = StyleSheet.create({
dropdown: {
height: 50,
borderColor: 'gray',
borderWidth: 0.5,
borderRadius: 8,
paddingHorizontal: 8,
},
row: {
flexDirection: 'row',
flexWrap: 'wrap',
},
});
I've got a dropdown, and setValue to setValue of value to the selection of the dropdown. I've also got a text element that should display the value whenever the dropdown is changed, but when I change the dropdown it doesn't update the Text and it stays blank? This is the dropdown I am using https://www.npmjs.com/package/react-native-element-dropdown
const App = () => {
const [products, setProducts] = useState<ProductType[] | []>([]);
const [open, setOpen] = useState(false);
const [value, setValue] = useState(' ');
const [isFocus, setIsFocus] = useState(false);
const getProducts = async () => {
try {
const response = await axios.get('http://192.168.1.32:3000/api/products');
setProducts(response.data);
} catch (error) {
// handle error
alert('no');
}
};
React.useEffect(() => {
getProducts();
}, []);
return (
<>
<AppBar
title="App"
trailing={props => (
<Button
variant="text"
title="ADD"
compact
//onPress={getProducts}
style={{marginEnd: 4}}
{...props}
/>
)}
/>
<Dropdown
style={[styles.dropdown, isFocus && {borderColor: 'blue'}]}
data={products}
search
maxHeight={300}
labelField="product"
valueField="aisle"
placeholder={!isFocus ? 'Select item' : '...'}
searchPlaceholder="Search..."
value={value}
onFocus={() => setIsFocus(true)}
onBlur={() => setIsFocus(false)}
onChange={item => {
setValue(item.value);
setIsFocus(false);
}}
/>
<TextInput label="quantity" variant="standard" />
<Text>value: {value}</Text>
</>
);
};
export default App;
const styles = StyleSheet.create({
dropdown: {
height: 50,
borderColor: 'gray',
borderWidth: 0.5,
borderRadius: 8,
paddingHorizontal: 8,
},
row: {
flexDirection: 'row',
flexWrap: 'wrap',
},
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
onChange
函数正在触发,并且值setValue
正在接收是问题,而不是下拉级
component。我已经重新创建了您的代码(为简单起见,删除了Appbar),并用 this代码工作:
我看到
aisle
是您给予valuefield
的值。您的意思是做setValue(item.aisle)
而不是setValue(item.value)
?如果
item.Value
是您想要的,则验证它不是一个空字符串。将其记录在您的onChange
函数中。I think that the
onChange
function is being fired and the valuesetValue
is receiving is the problem, not theDropdown
component.I've recreated your code (removed the AppBar for simplicity) and replaced your api with dummyjson and this code worked:
I see that
aisle
is the value you gave to thevalueField
. Did you mean to dosetValue(item.aisle)
and notsetValue(item.value)
?If
item.value
is what you want then verify that it isnt an empty string. Log it in youronChange
function.