简体   繁体   English

如何动态更改React Bootstrap模式的内容?

[英]How do I dynamically change the content of a React Bootstrap modal?

I'm trying to change the content of the modal after it has mounted, but I'm not able to find the correct nodes to change. 我试图在安装后更改模态的内容,但我无法找到要更改的正确节点。 I've attached refs to the nodes I'm interested in and try to alter them in componentDidMount(). 我已将refs附加到我感兴趣的节点上,并尝试在componentDidMount()中更改它们。 But the nodes are not found -- comes up as null. 但是找不到节点 - 出现为null。

 var Modal = ReactBootstrap.Modal; const MyModal = React.createClass({ getInitialState() { return { showModal: false }; }, close() { this.setState({ showModal: false }); }, open() { this.setState({ showModal: true }); }, componentDidMount() { var theNode = ReactDOM.findDOMNode(this.refs.bigPic); var theOtherNode = ReactDOM.findDOMNode(this.refs.bigPicInfo); theNode.src = 'http://big-pic.png'; theOtherNode.innerHTML = "<strong> Something here</strong>"; }, render() { return ( <div> <Modal show={this.state.showModal} onHide={this.close}> <Modal.Header closeButton> <Modal.Title></Modal.Title> </Modal.Header> <Modal.Body> <div><img ref="bigPic" src="" /></div> </Modal.Body> <Modal.Footer> <p ref="bigPicInfo"></p> </Modal.Footer> </Modal> </div> ); } }); ReactDOM.render(<MyModal/>, document.getElementById("my-modal")); 

Dynamic content in React is driven by component state, the same way you're using this.state.showModal to dynamically make the modal appear or not. React中的动态内容由组件状态驱动,就像使用this.state.showModal动态生成模式一样。 Anything that can possibly change should have a default setting in getInitialState , then call this.setState() with your new values.. this will trigger your component to re-render. 任何可能改变的东西都应该在getInitialState有一个默认设置,然后用你的新值调用this.setState() ..这将触发你的组件重新渲染。

const MyModal = React.createClass({

  getInitialState() {
    return { 
      showModal: false,
      bigPicSrc: '',
      infoContent: ''
    }
  },

  ...

  componentDidMount() {
    this.setState({ 
      bigPicSrc: 'http://big-pic.png'
      infoContent: <strong>Something here</strong> // not a string, but a component
    })
  },

  render() {
    return (
      <Modal show={this.state.showModal} onHide={this.close}>
        <Modal.Header closeButton>
          <Modal.Title></Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <div><img ref="bigPic" src={this.state.bigPicSrc} /></div>
        </Modal.Body>
        <Modal.Footer>
          <p ref="bigPicInfo">{this.state.infoContent}</p>
        </Modal.Footer>
      </Modal>
    )
  }
})

I use node and react 16, before I was learn little more of Bootstrap, and now collecting my knowledge about bout react and bootstrap. 在我学习Bootstrap之前,我使用node并做出反应16,现在我收集了关于bout react和bootstrap的知识。 On next away I make modal: First I am put CDN links with Bootstrap css and js, and jquery from index.html from public folder. 接下来,我制作模态:首先,我将来自bootstrap css和js的CDN链接,以及来自index.html的公共文件夹中的jquery。 Next make folder for components from SRC folder of ny app. 接下来从ny app的SRC文件夹中创建组件的文件夹。 Next step I put bootstrap code from new file example modal.js and change bootstrap class from clssName in React. 下一步我从新文件示例modal.js中放入bootstrap代码,并从React中的clssName更改bootstrap类。 And modal worked Klick on this text for show modal 并且modal在这个文本上使用了Klick for show modal

. And think for change content of modal must use data some data of Json file there You must connect id field of Json data and ref or id tigger event with you calling this modal. 并且认为模态的更改内容必须使用数据Json文件的一些数据你必须连接调用这个模态连接Json数据的id字段和ref或id tigger事件。 Ever different event calling Id field from data.json. 从data.json调用Id字段的不同事件。 I thing for that is best use switch case for easy choose. 我最好使用开关盒,方便选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM