Javascript 反应本机选取器移回第一项

Javascript 反应本机选取器移回第一项,javascript,react-native,picker,Javascript,React Native,Picker,这就像预期的一样,选择器停留在所选项目上 <Picker selectedValue={this.state.person} onValueChange={(itemValue) => this.setState({person: itemValue})} style={styles.picker}> {Object.keys(coWorkers) .map((result, index) => &l

这就像预期的一样,选择器停留在所选项目上

<Picker
    selectedValue={this.state.person}
    onValueChange={(itemValue) => this.setState({person: itemValue})}
    style={styles.picker}>
    {Object.keys(coWorkers)
        .map((result, index) =>
            <Picker.Item
                label={`${coWorkers[result].name}(${coWorkers[result].likes})`}
                value={coWorkers[result].name}
                key={index}
            />
        )
    }
</Picker>

但是,现在选择器跳回顶部(
this.state
正在正确更新)。

选择器的属性值类型应为字符串或整数。网站上的文档不清楚,但您可以在这里的Picker源代码注释中看到它

它对selectedValue和picker items值进行简单的相等检查,以将其转换为本机Pickeros

尽管内容相同,但对象
this.state.selectedValue
和matching
coorders[result]
是不同的对象


您可以为数组中的每个项目生成一些唯一的ID,并使用这些ID来查找对象。

您的itemValue.name与您的同事[结果]不匹配。

如果其他人有此问题,并且上述解决方案不起作用,可能是因为您没有将
selectedValue
标志设置为:

// You can replace the null with an actual value in your array(check to see it's exact)
const [toLanguage, setToLanguage] = useState(null); 


selectedValue={toLanguage}

链接的源代码中的
的PropType是
任何
,而不是字符串/integer@lfkwtz你不是这个意思吗?我的回答已经两年多了。很明显,它已经更新,不再相关。
// You can replace the null with an actual value in your array(check to see it's exact)
const [toLanguage, setToLanguage] = useState(null); 


selectedValue={toLanguage}