将本机 Picker 项目列表绑定到获取请求并选择一个项目
React native Picker item list binding with fetch request and selecting an item
需要将从 useEffect() 中的获取请求中获取的运动列表绑定到 react-native Picker,并将 select 来自 picker.item 的运动绑定到变量。使用基于 class 的组件找到 。在具有网络绑定和项目 selection
的功能组件中需要一个解决方案
import React, { useState, useEffect } from 'react'
import { Picker } from '@react-native-picker/picker';
// Functional Component
export default function PlayerRegistration() {
// Picker.item List
const [listofsports, setListOfSports] = useState([])
// Selected Item variable
const [selectedItem, setSelectedSport] = useState()
// Fetch Function
const getListOfPlayersList = () => {
const api = API();
console.log('working backendapi', api)
const sportscentersapi = `${api}/sports`;
const test = fetch(sportscentersapi, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
//console.log(data),
setListOfSports({ listofsports: data })
});
}
// ComponentDidmount or ComponentDidUpdate Equivalent
useEffect(() => {
getListOfPlayersList();
// Returning list looks similar to this.
//[{ "idsport": 1, "sportname": "Badminton" }, { "idsport": 2,"sportname": "Golf"}]
});
// JSX
return ( <Picker
selectedValue={selectedItem}
onValueChange={(itemValue, itemIndex) =>
setSelectedSport(itemValue)
}>
{
listofsports.map((item, index) => {
return (<Picker.Item
key={index}
label={`${item.sportname.toString()}`}
value={`${item.sportname}`} />)
})
}
</Picker>)
}
参考这个 我得到了一个按预期工作的答案。
import React, { useState, useEffect } from "react";
import { Picker } from "@react-native-picker/picker";
// Functional Component
export default function PlayerRegistration() {
// Picker.item List
const [listofSports, setlistofSports] = useState([]);
// Selected Item variable
const [selectedValue, setSelectedValue] = useState("");
// Fetch Function
const getListOfPlayersList = async () => {
const api = 'http://localhost:3000';
console.log("working backendapi", api);
const sportscentersapi = `${api}/sports`;
const test = fetch(sportscentersapi, {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then((response) => response.json())
.then((json) => setlistofSports(json.data))
.catch((error) => console.error(error));
// Incase if you need static json could use this
// return [
// { idsport: 1, sportname: "Badminton" },
// { idsport: 2, sportname: "Golf" }
// ];
};
// ComponentDidmount or ComponentDidUpdate Equivalent
useEffect(() => {
getListOfPlayersList();
}, []);
const renderSportsList = () => {
return listofSports.map((sport) => {
return <Picker.item label={sport.idsport}
value={sport.sportname} />;
});
};
// JSX
return (
<Picker
selectedValue={selectedValue}
style={{ height: 40, width: 150 }}
onValueChange={(itemValue, itemIndex) => {
setSelectedValue(itemValue);
}}
>
{renderSportsList()}
</Picker>
);
}
需要将从 useEffect() 中的获取请求中获取的运动列表绑定到 react-native Picker,并将 select 来自 picker.item 的运动绑定到变量。使用基于 class 的组件找到
import React, { useState, useEffect } from 'react'
import { Picker } from '@react-native-picker/picker';
// Functional Component
export default function PlayerRegistration() {
// Picker.item List
const [listofsports, setListOfSports] = useState([])
// Selected Item variable
const [selectedItem, setSelectedSport] = useState()
// Fetch Function
const getListOfPlayersList = () => {
const api = API();
console.log('working backendapi', api)
const sportscentersapi = `${api}/sports`;
const test = fetch(sportscentersapi, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
//console.log(data),
setListOfSports({ listofsports: data })
});
}
// ComponentDidmount or ComponentDidUpdate Equivalent
useEffect(() => {
getListOfPlayersList();
// Returning list looks similar to this.
//[{ "idsport": 1, "sportname": "Badminton" }, { "idsport": 2,"sportname": "Golf"}]
});
// JSX
return ( <Picker
selectedValue={selectedItem}
onValueChange={(itemValue, itemIndex) =>
setSelectedSport(itemValue)
}>
{
listofsports.map((item, index) => {
return (<Picker.Item
key={index}
label={`${item.sportname.toString()}`}
value={`${item.sportname}`} />)
})
}
</Picker>)
}
参考这个
import React, { useState, useEffect } from "react";
import { Picker } from "@react-native-picker/picker";
// Functional Component
export default function PlayerRegistration() {
// Picker.item List
const [listofSports, setlistofSports] = useState([]);
// Selected Item variable
const [selectedValue, setSelectedValue] = useState("");
// Fetch Function
const getListOfPlayersList = async () => {
const api = 'http://localhost:3000';
console.log("working backendapi", api);
const sportscentersapi = `${api}/sports`;
const test = fetch(sportscentersapi, {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then((response) => response.json())
.then((json) => setlistofSports(json.data))
.catch((error) => console.error(error));
// Incase if you need static json could use this
// return [
// { idsport: 1, sportname: "Badminton" },
// { idsport: 2, sportname: "Golf" }
// ];
};
// ComponentDidmount or ComponentDidUpdate Equivalent
useEffect(() => {
getListOfPlayersList();
}, []);
const renderSportsList = () => {
return listofSports.map((sport) => {
return <Picker.item label={sport.idsport}
value={sport.sportname} />;
});
};
// JSX
return (
<Picker
selectedValue={selectedValue}
style={{ height: 40, width: 150 }}
onValueChange={(itemValue, itemIndex) => {
setSelectedValue(itemValue);
}}
>
{renderSportsList()}
</Picker>
);
}