Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

app-root modal component

// These two containers are siblings in the DOM
const appRoot = document.getElementById('app-root');
const modalRoot = document.getElementById('modal-root');

class Modal extends React.Component {
  constructor(props) {
    super(props);
    this.el = document.createElement('div');
  }

  componentDidMount() {
    // The portal element is inserted in the DOM tree after
    // the Modal's children are mounted, meaning that children
    // will be mounted on a detached DOM node. If a child
    // component requires to be attached to the DOM tree
    // immediately when mounted, for example to measure a
    // DOM node, or uses 'autoFocus' in a descendant, add
    // state to Modal and only render the children when Modal
    // is inserted in the DOM tree.
    modalRoot.appendChild(this.el);
  }

  componentWillUnmount() {
    modalRoot.removeChild(this.el);
  }

  render() {
    return ReactDOM.createPortal(      this.props.children,      this.el    );  }
}

class Parent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {clicks: 0};
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {    // This will fire when the button in Child is clicked,    // updating Parent's state, even though button    // is not direct descendant in the DOM.    this.setState(state => ({      clicks: state.clicks + 1    }));  }
  render() {
    return (
      <div onClick={this.handleClick}>        <p>Number of clicks: {this.state.clicks}</p>
        <p>
          Open up the browser DevTools
          to observe that the button
          is not a child of the div
          with the onClick handler.
        </p>
        <Modal>          <Child />        </Modal>      </div>
    );
  }
}

function Child() {
  // The click event on this button will bubble up to parent,  // because there is no 'onClick' attribute defined  return (
    <div className="modal">
      <button>Click</button>    </div>
  );
}

ReactDOM.render(<Parent />, appRoot);
Comment

PREVIOUS NEXT
Code Example
Javascript :: dynamically populate vue material table 
Javascript :: %20find%20all%20docs%20that%20have%20at%20least%20two%20name%20array%20elements_ 
Javascript :: check if anagram 
Javascript :: fastselect clear select 
Javascript :: sub_total.toFixed is not a function 
Javascript :: regular expression 010+100 answer 
Javascript :: how to pass custom parameter onchage 
Javascript :: "Lua" 
Javascript :: serverless unsupported function event 
Javascript :: how to use variable key as dictionary key in javascript 
Javascript :: delete all properties from an javascript object second solution 
Javascript :: change event when click multiple revit api 
Javascript :: fly: javascript fly 
Javascript :: array object sort by date 
Javascript :: how we use usefef in map function 
Javascript :: maximum element in an array javascript 
Javascript :: verificar radio selected 
Javascript :: js find selected input 
Javascript :: jquery select text with event target 
Javascript :: javascript bitset 
Javascript :: https://social-network.samuraijs.com/article/faq_po_api 
Javascript :: how is coa useful npm 
Javascript :: javascript multiple enventListeners in one 
Javascript :: scale sprite matter.js 
Javascript :: JavaScript : Generate random element of an array : 
Javascript :: online regex generator based on string 
Javascript :: Dublin, Edinburgh, Lisbon, London getcurrentdatetime in angularjs 
Javascript :: Literal string with a variable inserted 
Javascript :: React Native - Trigger Media Scanner 
Javascript :: flyweight 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =