我正在尝试在Picker元素内的数组中显示项目。我在显示信息时遇到麻烦。我认为这是因为我正在从Promise中从Firebase加载信息,而react-native在加载信息之前会呈现Picker元素。我不确定如何解决这个问题。
这是我从Firebase调用信息的地方:
loadProjects = (currUser) => {
let projectRef = firebase.firestore().collection("Projects");
let userLoc = "Users/" + currUser + "/Projects";
let userRef = firebase.firestore().collection(userLoc);
//Gets every single document in the Users 'Projects' collection and adds it to an array in the state.
userRef.get().then((documentSnapshot) => {
documentSnapshot.forEach((document) => {
this.state.projectsArr.push(document.id);
})
})
.catch((error) => {
alert(error);
});
};
我在render方法之后调用它:
render(){
const currUser = this.props.navigation.getParam('currentUser', 'NO-USER');
this.loadProjects(currUser);
这是我的Picker元素:
<Picker
selectedValue = {this.state.selectedProject}
onValueChange={(itemValue) => this.setState({selectedProject : itemValue})}
>
{
this.state.projectsArr.map((item, index) => {
return (<Picker.Item label={item} value={item} key={index}/>);
})
}
</Picker>
非常感谢您的帮助!
答案 0 :(得分:1)
如果将其放在渲染中,则可以进行多个调用。如果您需要一次调用,请不要将其放入渲染器。
如果收到的数据很好,则可以使用import requests
from bs4 import BeautifulSoup
url='https://classic.comunio.de/login.phtml'
header={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
payload={'login':'yourname','pass':'yourpass'}
s=requests.Session()
page = s.post(url,data=payload,headers=header)
soup = BeautifulSoup(page.content,'html.parser')
result = soup.find('div',attrs={'id':"manager"}).get_text()
print(result)
componentDidmount()
答案 1 :(得分:1)
您设置错误的状态
userRef.get().then((documentSnapshot) => {
const newProjectsArr = []
documentSnapshot.forEach((document) => {
newProjectsArr .push(document.id);
})
this.setState({
projectsArr: newProjectsArr
}}
})