鍍金池/ 問答/HTML/ react componentDidMount時機問題

react componentDidMount時機問題

componentDidMount是組件確實渲染成了dom后觸發(fā),如果我在這時候append一個絕對定位top0的有顏色的div,預(yù)期效果應(yīng)該是先顯示react組件,后立馬顯示紅色div但是實際是直接顯示紅色div了,都沒有閃一下的效果,這是為何?

class App extends Component {
  constructor(){
    super();
  }
  componentDidMount() {
    const div=document.createElement('div');
    div.style='position:absolute;height:100px;width:100vw;background:blue;top:0';
    document.body.appendChild(div)
  }
  render(){  
    return (
      <div style={{background:'red'}}>hello world</div>
    )
  }
}
回答
編輯回答
悶油瓶

render部分寫法有誤

render() {
    return (
      <div style={{background:'red'}}>hello world</div>
    )
 }

現(xiàn)在應(yīng)該可以先看到紅色,然后再看到藍(lán)色了

2017年11月11日 15:29
編輯回答
不舍棄

你為什么會覺得現(xiàn)代計算機有這么慢?
想看效果自己用setTimeout設(shè)置延遲。

2017年6月14日 14:25
編輯回答
孤毒

同1樓,這個過程肉眼是看不出來的。就想同理如果在這個生命在后期中調(diào)用api來獲取數(shù)據(jù),實際是調(diào)用了2次,但是在瀏覽器中,是根本看不出來的。

2018年5月5日 23:15