2017-10-17 46 views
0

我使用了React-Bootstrap Modal,不明白爲什麼Foo組件渲染兩次?React-Bootstrap模態問題?

import { Modal, Button } from 'react-bootstrap'; 

    const Foo = (props) => { 
     console.log(‘a’); 
return(
     <div>foo</div> 
    ) 
    } 

    class Example extends Component { 
     render() { 
     return(
      <Modal show={true}> 
       <Foo /> 
      </Modal> 
     ) 
     } 
    } 
+0

你是什麼意思執行兩次? –

+0

你確定嗎?您是否嘗試重建或重新運行熱重新加載?看看代碼,這不能被渲染兩次。 – Fawaz

回答

1

貌似引導模式設置狀態下,當模式進入(即使顯示初始設置爲true,你有以上)。請參閱https://github.com/react-bootstrap/react-bootstrap/blob/master/src/Modal.js#L202-L209。因此,將再次觸發渲染,導致Foo渲染兩次。

編輯:你實際上必須一路回到React-transition-group來理解爲什麼onEntering函數實際被調用。 https://github.com/reactjs/react-transition-group/blob/master/src/Transition.js#L192。在componentDidMount上調用this.updateStatus,最終調用onEntering。

+0

由於內部組件是設置狀態。你正在設置你的組件的狀態,Modal也在內部設置狀態,因此是雙渲染。 –

+1

如果在Bootstrap Modal上設置animation = {false},則Foo組件只渲染一次。謝謝您的幫助。 – George