在 React Native Picker 中选择第一项时,有没有办法调用方法?
Is there a way to call method when first item is picked in React Native Picker?
编辑
functionToRedirect(itemValue)
is a function that gets called when an item in the picker is selected and it redirects to a new screen prepopulating some values in the screen based on 'itemValue'
.
However, functionToRedirect(itemValue)
is not being called when the same value is selected which I assume is because the value didn't change and thus onValueChange()
will not be fired.
有没有办法让我调用 functionToRedirect(itemValue)
即使值没有改变(即选择初始值时)尝试重定向到新屏幕?
<Picker
style={styles.picker}
itemStyle={styles.pickerItem}
mode={"dialog"}
selectedValue={this.props.value}
onValueChange={(itemValue) => {
functionToRedirect(itemValue);
}
}>
{this.state.pickerItems}
</Picker>
使用自定义选择器怎么样? Nativebase,ReactNative UI 组件的自定义实现可以为您提供自定义风格的组件。
他们已经实现 Picker 使用 RN Picker 作为在模式 window 中打开的项目平面列表的包装器,您可以通过它相应地 select 项目。
我想到的解决方案是传递 selectedValue null,这样下拉列表中第一个显示的选项就是下拉标题。
解决方案如下所示
<Picker
style={styles.picker}
itemStyle={styles.pickerItem}
mode={"dialog"}
selectedValue={null}
onValueChange={(itemValue) => {
this.props.onChange(itemValue)
}}
>
{this.state.pickerItems}
</Picker>
编辑
functionToRedirect(itemValue)
is a function that gets called when an item in the picker is selected and it redirects to a new screen prepopulating some values in the screen based on 'itemValue'
.
However, functionToRedirect(itemValue)
is not being called when the same value is selected which I assume is because the value didn't change and thus onValueChange()
will not be fired.
有没有办法让我调用 functionToRedirect(itemValue)
即使值没有改变(即选择初始值时)尝试重定向到新屏幕?
<Picker
style={styles.picker}
itemStyle={styles.pickerItem}
mode={"dialog"}
selectedValue={this.props.value}
onValueChange={(itemValue) => {
functionToRedirect(itemValue);
}
}>
{this.state.pickerItems}
</Picker>
使用自定义选择器怎么样? Nativebase,ReactNative UI 组件的自定义实现可以为您提供自定义风格的组件。 他们已经实现 Picker 使用 RN Picker 作为在模式 window 中打开的项目平面列表的包装器,您可以通过它相应地 select 项目。
我想到的解决方案是传递 selectedValue null,这样下拉列表中第一个显示的选项就是下拉标题。
解决方案如下所示
<Picker
style={styles.picker}
itemStyle={styles.pickerItem}
mode={"dialog"}
selectedValue={null}
onValueChange={(itemValue) => {
this.props.onChange(itemValue)
}}
>
{this.state.pickerItems}
</Picker>