Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 792
usser441251343234 Avatar asked Jul 13 '16 01:07

usser441251343234


People also ask

How dynamically add component in react native?

Step 1: Create a new react native project, if you don't know how to create a new project in react native just follow this tutorial. Step 2: Open App. js File in your favorite code editor and erase all code and follow this tutorial. Step 3: Through react , react-native packages import all required components.

Which method is used to add components or elements to the DOM in React?

AppendChild. The simplest, most well-known method of appending an element to the DOM is certainly the appendChild() .


3 Answers

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); 
},
like image 158
hsiung Avatar answered Oct 05 '22 23:10

hsiung


Maybe react-portal can help you.

From the docs:

transports its child into a new React component and appends it to the document.body (creates a new independent React tree)

I use a lot (for dialogs/modals and menu popups). Very useful and lightweight.

like image 35
mrlew Avatar answered Oct 06 '22 01:10

mrlew


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 :).

like image 45
gaperton Avatar answered Oct 06 '22 00:10

gaperton