0
我有以下反應本地程序:即使在我調用setState之後組件還會重新渲染?
class offerDetail extends Component {
constructor(props) {
super(props);
this.state = {
phone: null,
logo: null,
instagram: null
};
const { name } = this.props.doc.data();
this.ref = firebase.firestore().collection('companies').doc(name);
}
componentWillMount() {
let docObject = null;
this.ref.get().then(doc => {
let docObject = doc.data();
console.log(docObject); -----------------------------1
});
this.setState(
docObject
);
console.log(this.state); --------------------------------2
}
render() {
console.log(this.state);--------------------3
...
......
我有3個實例,其中我打印到控制檯。只有在實例編號1中它打印非空值,但是,在實例2和3中它打印空值。爲什麼實例2在setState之後立即調用時打印出null?
它是不是正確設置狀態,爲什麼?
,如果我想獲得什麼它從渲染方法裏面?正如你看到它在位置3處返回null。那是我真正需要它的地方嗎? –
你將不得不執行一些檢查,「if(this.state.docObject){// loaded} else {// still null}'。 – Dan