简体   繁体   English

映射选择器项目时如何使用 Picker 更改选择器值?

[英]How to make change on picker value with Picker when the picker items are mapped?

I don't success to change the value selected of the picker.我没有成功更改选择器的选定值。 When I click on other value it change correctly and after 0.2 sec it put the first value again.当我单击其他值时,它会正确更改,并在 0.2 秒后再次输入第一个值。 I don't know why...我不知道为什么...

Could you help me please?请问你能帮帮我吗?

 const [espaceSelected, setEspaceSelected] = useState(null);

    useEffect(() => {
        getEspaces().then(data => {
            setEspaces(data);
            AsyncStorage.setItem('id_espace',espaces[0].id_espace);
            setEspaceSelected(espaces[2].name);
        })
    }, []);

...
return ( ...
  <Picker selectedValue={espaceSelected ? espaceSelected : null} onValueChange={(value) => { setEspaceSelected(value.name);}}>
                {espaces ? espaces.map(e => {
                    return (
                        <Picker.Item color="#7B65AE" label={e.name} value={e.id_espace} />);
                }) : null}
            </Picker>
);

Thanks in advance提前致谢

In the render code for the Picker you set the value to value={e.id_espace} and then try to access the value.name in the onValueChange , but the value is only the id and not the object having an id and the name .Picker的渲染代码中,您将值设置为value={e.id_espace}然后尝试访问value.name中的onValueChange ,但该value只是id而不是具有idname的 object 。

For example you can fix it, by using the name everywhere and not mixing it with the id or the object:例如,您可以通过在任何地方使用name而不将其与 id 或 object 混合来修复它:

...onValueChange={(name) => { setEspaceSelected(name); }} ...
...
<Picker.Item color="#7B65AE" label={e.name} value={e.name} />

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

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