React Native Picker: 设置和获取不同的选择器数值

huangapple go评论75阅读模式
英文:

React Native Picker: Set and Get different Picker Values

问题

我正在使用React Native Picker,并创建了两个状态:userTypename

然而,有两种userTypeFreelancer(自由职业者)、Client(客户)。对于每种userType都有两个name,它们分别是:Freelancer 1Freelancer 2Client 1Client 2

目前,我有两个选择器(Picker)。一个用于userType,另一个用于name。无论选择了哪种userType,第二个选择器都显示所有的name,即Freelancer 1、Freelancer 2、Client 1、Client 2。

但这是我想要实现的内容:

如何实现:不要显示所有的name,而是根据所选择的userType来显示特定的name。例如:如果userType === Freelancer,那么name选择器应该只显示Freelancer 1Freelancer 2。如果userType === Client,那么name选择器应该只显示Client 1Client 2

下面提供了代码片段:

constructor(props) {
    super(props);
    this.state = {
        userType: '',
        name: '',
    }
}

render() {
    return (
        <View>
            <View style={{ flexDirection: 'row' }}>
                <Text style={styles.titleStyle}>User Type</Text>
                <View style={styles.pickerStyle}>
                    {<Picker
                        mode='dropdown'
                        selectedValue={this.state.userType}
                        onValueChange={(itemValue, itemIndex) =>
                            this.setState({ userType: itemValue })
                        }>
                        <Picker.Item label="Select User Type" value="" />
                        <Picker.Item label="Freelancer" value="Freelancer" />
                        <Picker.Item label="Client" value="Client" />
                    </Picker>}
                </View>
            </View>
            <View style={{ flexDirection: 'row' }}>
                <Text style={styles.titleStyle}>Name</Text>
                <View style={styles.pickerStyle}>
                    {<Picker
                        mode='dropdown'
                        selectedValue={this.state.name}
                        onValueChange={(itemValue, itemIndex) =>
                            this.setState({ name: itemValue })
                        }>
                        <Picker.Item label="Please Select" value="" />
                        {this.state.userType === 'Freelancer' && (
                            <>
                                <Picker.Item label="Freelancer 1" value="Freelancer 1" />
                                <Picker.Item label="Freelancer 2" value="Freelancer 2" />
                            </>
                        )}
                        {this.state.userType === 'Client' && (
                            <>
                                <Picker.Item label="Client 1" value="Client 1" />
                                <Picker.Item label="Client 2" value="Client 2" />
                            </>
                        )}
                    </Picker>}
                </View>
            </View>
        </View>
    );
};

应用程序截图:

React Native Picker: 设置和获取不同的选择器数值

英文:

I am working with React Native Picker and I have created two states: userType and name.

However, there are two userType: Freelancer, Client. There are also two name for each userType, and these are: Freelancer 1, Freelancer 2, and Client 1, Client 2.

Currently, I have two pickers. One has the userType and the other has name. Regardless of the userType selected, the 2nd picker shows all the name i.e Freelancer 1, Freelancer 2, Client 1, Client 2.

But this is what I am trying to achieve:

> HOW TO: Instead of showing all the name, I want to show specific name depending on the userType selected. For example: if userType === Freelancer, then name picker should display only Freelancer 1, Freelancer 2. And, if userType === Client, then name picker should display only Client 1, Client 2.

Code snippet Provided Below:

constructor(props) {
super(props);
this.state = {
userType: &#39;&#39;,
name: &#39;&#39;,
}
}
render() {
return (
&lt;View&gt;
&lt;View style={{ flexDirection: &#39;row&#39; }}&gt;
&lt;Text style={styles.titleStyle}&gt;User Type&lt;/Text&gt;
&lt;View style={styles.pickerStyle}&gt;
{&lt;Picker
mode=&#39;dropdown&#39;
selectedValue={this.state.userType}
onValueChange={(itemValue, itemIndex) =&gt;
this.setState({ userType: itemValue })
}&gt;
&lt;Picker.Item label=&quot;Select User Type&quot; value=&quot;&quot; /&gt;
&lt;Picker.Item label=&quot;Freelancer&quot; value=&quot;Freelancer&quot; /&gt;
&lt;Picker.Item label=&quot;Client&quot; value=&quot;Client&quot; /&gt;
&lt;/Picker&gt;}
&lt;/View&gt;
&lt;/View&gt;
&lt;View style={{ flexDirection: &#39;row&#39; }}&gt;
&lt;Text style={styles.titleStyle}&gt;Name&lt;/Text&gt;
&lt;View style={styles.pickerStyle}&gt;
{&lt;Picker
mode=&#39;dropdown&#39;
selectedValue={this.state.name}
onValueChange={(itemValue, itemIndex) =&gt;
this.setState({ name: itemValue })
}&gt;
&lt;Picker.Item label=&quot;Please Select&quot; value=&quot;&quot; /&gt;
&lt;Picker.Item label=&quot;Freelancer 1&quot; value=&quot;Freelancer 1&quot; /&gt;
&lt;Picker.Item label=&quot;Freelancer 2&quot; value=&quot;Freelancer 2&quot; /&gt;
&lt;Picker.Item label=&quot;Client 1&quot; value=&quot;Client 1&quot; /&gt;
&lt;Picker.Item label=&quot;Client 2&quot; value=&quot;Client 2&quot; /&gt;
&lt;/Picker&gt;}
&lt;/View&gt;
&lt;/View&gt;
&lt;/View&gt;
);
};

App Screenshot:
React Native Picker: 设置和获取不同的选择器数值

答案1

得分: 1

你应该有一个根据用户类型返回用户名的函数。

renderUserNames() {
    if (this.state.userType === 'Freelancer') {
        return [
            <Picker.Item label="自由职业者 1" value="自由职业者 1" />,
            <Picker.Item label="自由职业者 2" value="自由职业者 2" />
        ];
    } else {
        return [
            <Picker.Item label="客户 1" value="客户 1" />,
            <Picker.Item label="客户 2" value="客户 2" />
        ];
    }
}

然后在render函数中可以这样调用

render() {
    let options = this.renderUserNames();
    return (
        <View>
            <View style={{ flexDirection: 'row' }}>
                <Text style={styles.titleStyle}>用户类型</Text>
                <View style={styles.pickerStyle}>
                    <Picker
                        mode='dropdown'
                        selectedValue={this.state.userType}
                        onValueChange={(itemValue, itemIndex) =>
                            this.setState({ userType: itemValue })
                        }>
                        <Picker.Item label="选择用户类型" value="" />
                        <Picker.Item label="自由职业者" value="自由职业者" />
                        <Picker.Item label="客户" value="客户" />
                    </Picker>
                </View>
            </View>
            <View style={{ flexDirection: 'row' }}>
                <Text style={styles.titleStyle}>姓名</Text>
                <View style={styles.pickerStyle}>
                    <Picker
                        mode='dropdown'
                        selectedValue={this.state.name}
                        onValueChange={(itemValue, itemIndex) =>
                            this.setState({ name: itemValue })
                        }>
                        <Picker.Item label="请选择" value="" />
                        {options}
                    </Picker>
                </View>
            </View>
        </View>
    );
};

这是你的代码的中文翻译。

英文:

you should have function that return usernames based on user type

renderUserNames() {
if(this.state.userType==&#39;Freelancer&#39;){
return [&lt;Picker.Item label=&quot;Freelancer 1&quot; value=&quot;Freelancer 1&quot; /&gt;,
&lt;Picker.Item label=&quot;Freelancer 2&quot; value=&quot;Freelancer 2&quot; /&gt;]
}else{
return [&lt;Picker.Item label=&quot;Client 1&quot; value=&quot;Client 1&quot; /&gt;,
&lt;Picker.Item label=&quot;Client 2&quot; value=&quot;Client 2&quot; /&gt;]
}
}

then inside render function you can call i like

 render() {
let options=this.renderUserNames();
return (
&lt;View&gt;
&lt;View style={{ flexDirection: &#39;row&#39; }}&gt;
&lt;Text style={styles.titleStyle}&gt;User Type&lt;/Text&gt;
&lt;View style={styles.pickerStyle}&gt;
{&lt;Picker
mode=&#39;dropdown&#39;
selectedValue={this.state.userType}
onValueChange={(itemValue, itemIndex) =&gt;
this.setState({ userType: itemValue })
}&gt;
&lt;Picker.Item label=&quot;Select User Type&quot; value=&quot;&quot; /&gt;
&lt;Picker.Item label=&quot;Freelancer&quot; value=&quot;Freelancer&quot; /&gt;
&lt;Picker.Item label=&quot;Client&quot; value=&quot;Client&quot; /&gt;
&lt;/Picker&gt;}
&lt;/View&gt;
&lt;/View&gt;
&lt;View style={{ flexDirection: &#39;row&#39; }}&gt;
&lt;Text style={styles.titleStyle}&gt;Name&lt;/Text&gt;
&lt;View style={styles.pickerStyle}&gt;
{&lt;Picker
mode=&#39;dropdown&#39;
selectedValue={this.state.name}
onValueChange={(itemValue, itemIndex) =&gt;
this.setState({ name: itemValue })
}&gt;
&lt;Picker.Item label=&quot;Please Select&quot; value=&quot;&quot; /&gt;
{options}
&lt;/Picker&gt;}
&lt;/View&gt;
&lt;/View&gt;
&lt;/View&gt;
);
};

huangapple
  • 本文由 发表于 2020年1月6日 16:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定