下拉列表中的setValue不更新​​文本元素中的值变量。

发布于 2025-02-12 21:37:16 字数 2088 浏览 0 评论 0原文

我有一个下拉列表和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2025-02-19 21:37:16

我认为onChange函数正在触发,并且值setValue正在接收是问题,而不是下拉级 component。

我已经重新创建了您的代码(为简单起见,删除了Appbar),并用 this代码工作:

const App = () => {
  const [products, setProducts] = useState([]);
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(' ');
  const [isFocus, setIsFocus] = useState(false);

 
  const getProducts = async () => {
    try {
      const response = await axios.get('https://dummyjson.com/products');
      setProducts(response.data.products);
    } catch (error) {
      // handle error
      alert('Oh no','...');
    }
  };

  React.useEffect(() => {
    getProducts();
  }, []);


  return (
    <>
      <Dropdown
        style={[styles.dropdown, isFocus && {borderColor: 'blue'}]}
        data={products}
        search
        maxHeight={300}
        labelField="title"
        valueField="title"
        placeholder={!isFocus ? 'Select item' : '...'}
        searchPlaceholder="Search..."
        value={value}
        onFocus={() => setIsFocus(true)}
        onBlur={() => setIsFocus(false)}
        onChange={item => {
          setValue(item.title);
          setIsFocus(false);
        }}
      />
      <TextInput label="quantity" variant="standard" />
      <Text>value: {value}</Text>
    </>
  );
};
export default App;

我看到aisle是您给予valuefield的值。您的意思是做setValue(item.aisle)而不是setValue(item.value)

如果item.Value是您想要的,则验证它不是一个空字符串。将其记录在您的onChange函数中。

I think that the onChange function is being fired and the value setValue is receiving is the problem, not the Dropdown component.

I've recreated your code (removed the AppBar for simplicity) and replaced your api with dummyjson and this code worked:

const App = () => {
  const [products, setProducts] = useState([]);
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(' ');
  const [isFocus, setIsFocus] = useState(false);

 
  const getProducts = async () => {
    try {
      const response = await axios.get('https://dummyjson.com/products');
      setProducts(response.data.products);
    } catch (error) {
      // handle error
      alert('Oh no','...');
    }
  };

  React.useEffect(() => {
    getProducts();
  }, []);


  return (
    <>
      <Dropdown
        style={[styles.dropdown, isFocus && {borderColor: 'blue'}]}
        data={products}
        search
        maxHeight={300}
        labelField="title"
        valueField="title"
        placeholder={!isFocus ? 'Select item' : '...'}
        searchPlaceholder="Search..."
        value={value}
        onFocus={() => setIsFocus(true)}
        onBlur={() => setIsFocus(false)}
        onChange={item => {
          setValue(item.title);
          setIsFocus(false);
        }}
      />
      <TextInput label="quantity" variant="standard" />
      <Text>value: {value}</Text>
    </>
  );
};
export default App;

I see that aisle is the value you gave to the valueField. Did you mean to do setValue(item.aisle) and not setValue(item.value)?

If item.value is what you want then verify that it isnt an empty string. Log it in your onChange function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文