代码之家  ›  专栏  ›  技术社区  ›  George

反应引导模式渲染两次

  •  0
  • George  · 技术社区  · 8 年前

    我使用了React Bootstrap模式,不明白为什么Foo组件渲染了两次?

    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>
        )
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Steven Scaffidi    8 年前

    看起来引导模式在模式进入时设置状态(即使show最初设置为true,如上文所述)。请看 https://github.com/react-bootstrap/react-bootstrap/blob/master/src/Modal.js#L202-L209 . 因此,将再次触发渲染,导致Foo渲染两次。

    编辑:实际上,您必须一直返回到React transition group,以理解为什么实际调用了onenting函数。 https://github.com/reactjs/react-transition-group/blob/master/src/Transition.js#L192 . 在componentDidMount上。updateStatus被调用,它最终调用了onenting。