JavaScript How do you dynamically insert a React component into the DOM (for instance a dialog)? react dynamically insert component

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user.

Now, I can make this happen by creating a component and placing it in my markup. e.g.

<div> 
    <BunchOfComponents />
    <MoreComponents />

    <MyAlertDialog open={this.props.shouldShowAlert} />
</div>

And then make it show by controlling its open state via Redex or whatever.

However, what I'd like to do, it be able to not declare it in my markup and instead inject it into the dom via a function.

Something like...

myCoolFunction() {
    const alert = (
        <MyAlert
            open={true}
            msg="Hello World" 
        />
    )
    DOM.findNode('someID').insert(alert);  <-- fake API obviously   
}

Is it possible to dynamically append components like that?

Answer:1

This is the dirty way I do it at work (that didn't sound right...) http://codepen.io/matthsiung/pen/JKOpVW

I'm sure there's a better 'proper' way to do it but it cleans up after itself so it works for me.

Here are the key points:

Dialog trigger

//Your trigger function will render the dialog component to a newly created dummy div,
// while also passing it into the component as a prop

function showDialog() {
  var div = document.createElement('div');
  ReactDOM.render(
     <Dialog container={div}/>,
     document.body.appendChild(div)
  );
} 

Dialog component:

//When your dialog component closes it unmounts itself
close(){
  ReactDOM.unmountComponentAtNode(this.props.container);
},

//Before unmount it cleans up after itself by removing the dummy div   
componentWillUnmount() {
 document.body.removeChild(this.props.container); 
},
Answer:2

It's possible to inject some custom stuff with DOM manipulation inside of React rendered tree, if it's rendered as an empty node. Assuming you have somewhere in react's render:

<div id="you-know-id />

You can then find this node by id, and assign its innerHTML with something. More 'reactish' way of doing it is using the refs instead of id.

Usually, this stuff is being wrapped inside componentDidMount (where you insert) and componentWillUnmount (where you remove and unsubscribe from anything) methods.

Because, doing dirty tricks you probably want to avoid memory leaks. So, it's the way.

By the way, I don't think that it's the right way of approaching the problem you just described. What you could do instead, is something like this:

{ this.props.shouldShowAlert && <MyAlertDialog open={ true } /> }

And then in will be actually mounted when it's opened, and unmounted when it's closed.

Still, it's way safer to control injection with the state or prop member. You really need to know what you're doing when you do direct DOM manipulation. Say, wrapping some jQuery plugin :). Then you're fine, cause there are no other way :).

Answer:3

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

  1. reactjs state undefined in function
  2. reactjs undefined is not a function
  3. react js function undefined
  4. react state undefined in function

window.onerror = function(e){alert(e)}; function main(){ this.work = []; this.start_working = function() { try{ if(this.work.length > 0){ var y = this....

window.onerror = function(e){alert(e)}; function main(){ this.work = []; this.start_working = function() { try{ if(this.work.length > 0){ var y = this....

  1. problems with calling on iphone
  2. problems with calling 911
  3. problems with calling ios
  4. problems calling with o2
  5. problems with wifi calling
  6. problems with wifi calling on iphone 7
  7. problems with wifi calling iphone 6
  8. problems with cold calling
  9. problems with wifi calling iphone x
  10. problems with alexa calling
  11. problems with wifi calling on iphone 8
  12. problems with video calling
  13. problems with wifi calling on ee
  14. problems with wifi calling on iphone xr
  15. problems with wifi calling iphone
  16. problems with conference calling
  17. problems calling org.antlr.v4.gui.testrig.main(args)
  18. problems calling out iphone
  19. problems calling germany
  20. problems calling mexico

Which of these is normally seen as better when writing JavaScript. Foo, bar, and baz are specific to this function so won't be used anywhere else in other functions. function() { foo(); bar();...

Which of these is normally seen as better when writing JavaScript. Foo, bar, and baz are specific to this function so won't be used anywhere else in other functions. function() { foo(); bar();...

my question depends on the following scenario: I have an JS script, which adds a span element into a div. As the div has a text-align:middle;, the already existing spans are "jumping" to the left or ...

my question depends on the following scenario: I have an JS script, which adds a span element into a div. As the div has a text-align:middle;, the already existing spans are "jumping" to the left or ...