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