简体   繁体   English

反应原生中的布尔文本输入

[英]Boolean textInput in react native

How do we Boolean TextInput in react native.我们如何在本机中反应 Boolean TextInput。 I tried to use radio button or checked button but i was unable to send selected data to server side when user clicked submit button on the form.我尝试使用单选按钮或选中的按钮,但是当用户单击表单上的提交按钮时,我无法将选定的数据发送到服务器端。 If I use checked button the how do i know that button is checked by user ?如果我使用选中的按钮,我怎么知道用户选中了该按钮? I'm new to react native, it would be great help.我是本机反应的新手,这将是很大的帮助。 I'm reading docs of react native but using boolen text input it was difficult for me to understand.我正在阅读 react native 的文档,但使用 boolen 文本输入我很难理解。 Thank you.谢谢你。

I'm using Picker it would be great to know if there are better option我正在使用 Picker 很高兴知道是否有更好的选择

<Picker
            selectedValue={status}
            style={{ height: 50, width: 150 }}
            onValueChange={(itemValue) => setStatus(itemValue)}
          >
            <Picker.Item label="True" value="True" />
            <Picker.Item label="False" value="False" />
          </Picker>

Using Picker使用选择器

The label for the Picker.Item is the text that is show to the user, so that always needs to be a string . Picker.Itemlabel是显示给用户的文本,因此它始终需要是string But for the value prop, we can pass the boolean values true and false rather than a string .但是对于value prop,我们可以传递booleantruefalse而不是string That's it!就是这样!

<Picker
  selectedValue={status}
  style={{ height: 50, width: 150 }}
  onValueChange={setStatus}>
  <Picker.Item label="True" value={true} />
  <Picker.Item label="False" value={false} />
</Picker>

Other Options其他选项

A dropdown menu isn't the most intuitive way to select a true / false value.下拉菜单并不是选择true / false值的最直观方式。 I would say that a Switch is the best (though for some reason this isn't rendering right on expo Web).我会说Switch是最好的(尽管出于某种原因,这不能在 expo Web 上正确呈现)。

<Switch
  value={status}
  onValueChange={setStatus}
/>

Different components have different props, so check the docs to see if the value is a boolean , string , etc. For a single RadioButton , you use status={ status ? 'checked' : 'unchecked' }不同的组件有不同的道具,所以检查文档以查看值是否是booleanstring等。对于单个RadioButton ,您使用status={ status ? 'checked' : 'unchecked' } status={ status ? 'checked' : 'unchecked' } because it is controlled by the strings 'checked' and 'unchecked' . status={ status ? 'checked' : 'unchecked' }因为它由字符串'checked''unchecked'

Here's a demo showing four different options.这是一个演示,展示了四种不同的选项。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM