React bad practice to use ReactDOM.render and inject component?Function overloading in Javascript - Best practicesWhy is using “for…in” with array iteration a bad idea?JavaScript closure inside loops – simple practical exampleWhy is document.write considered a “bad practice”?Loop inside React JSXCan you force a React component to rerender without calling setState?What do these three dots in React do?Programmatically navigate using react routerHow to conditionally add attributes to React components?What is the difference between React Native and React?

What's the point of fighting monsters in Zelda BotW?

Do multi-engine jets need all engines with equal age to reduce asymmetry in thrust and fuel consumption arising out of deterioration?

Why didn't Doc believe Marty was from the future?

Is there a way to tell what frequency I need a PWM to be?

Is it unusual for a math department not to have a mail/web server?

Did ancient peoples ever hide their treasure behind puzzles?

Get contents before a colon

What is the following VRP?

Why can't I identify major and minor chords?

What is Soda Fountain Etiquette?

Why do IR remotes influence AM radios?

Group riding etiquette

Is this position a forced win for Black after move 14?

How can I reply to coworkers who accuse me of automating people out of work?

What is the sound/audio equivalent of "unsightly"?

Normalized Malbolge to Malbolge translator

Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?

Why does `buck` mean `step-down`?

Are sweatpants frowned upon on flights?

Is there a word or phrase that means "use other people's wifi or Internet service without consent"?

Why haven't the British protested Brexit as ardently like Hong Kongers protest?

What is a "hard problem" in the context of Mixed-integer programming?

Can I lend a small amount of my own money to a bank at the federal funds rate?

Why does Sauron not permit his followers to use his name?



React bad practice to use ReactDOM.render and inject component?


Function overloading in Javascript - Best practicesWhy is using “for…in” with array iteration a bad idea?JavaScript closure inside loops – simple practical exampleWhy is document.write considered a “bad practice”?Loop inside React JSXCan you force a React component to rerender without calling setState?What do these three dots in React do?Programmatically navigate using react routerHow to conditionally add attributes to React components?What is the difference between React Native and React?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















So I never really do this but I might have to for a certain problem I'm facing with a 3rd party library.



Basically, Is it bad to do something like



const fooEl = document.querySelector('.foo-class');
const newDiv = document.createElement('div');
const id = 'testingId';
newDiv.id = id;
foolEl.appendChild(newDiv);

ReactDOM.render(
<FooBarComponent />
document.getElementById(id)
);


If so is there any other way I can approach this? Basically i'm trying to insert a component into a DOM element.










share|improve this question



















  • 1





    What's the problem you're trying to solve?

    – djfdev
    Mar 27 at 22:04











  • This is perfectly valid code. Opinions about whether it's good or bad are off-topic for stack overflow

    – Fred Stark
    Mar 27 at 22:10











  • @FredStark, I think there is a difference between asking for a "best" practice (i.e an opinionated view of which method is better than any other method) and asking if an approach is "bad" (i.e if there are any known side-effects that can be avoided through another method). OP seems to be asking about the latter. The answer here, as you said yourself, is no - there's nothing inherently wrong with this code... but that doesn't make it opinionated.

    – Chris
    Mar 27 at 22:23

















0















So I never really do this but I might have to for a certain problem I'm facing with a 3rd party library.



Basically, Is it bad to do something like



const fooEl = document.querySelector('.foo-class');
const newDiv = document.createElement('div');
const id = 'testingId';
newDiv.id = id;
foolEl.appendChild(newDiv);

ReactDOM.render(
<FooBarComponent />
document.getElementById(id)
);


If so is there any other way I can approach this? Basically i'm trying to insert a component into a DOM element.










share|improve this question



















  • 1





    What's the problem you're trying to solve?

    – djfdev
    Mar 27 at 22:04











  • This is perfectly valid code. Opinions about whether it's good or bad are off-topic for stack overflow

    – Fred Stark
    Mar 27 at 22:10











  • @FredStark, I think there is a difference between asking for a "best" practice (i.e an opinionated view of which method is better than any other method) and asking if an approach is "bad" (i.e if there are any known side-effects that can be avoided through another method). OP seems to be asking about the latter. The answer here, as you said yourself, is no - there's nothing inherently wrong with this code... but that doesn't make it opinionated.

    – Chris
    Mar 27 at 22:23













0












0








0








So I never really do this but I might have to for a certain problem I'm facing with a 3rd party library.



Basically, Is it bad to do something like



const fooEl = document.querySelector('.foo-class');
const newDiv = document.createElement('div');
const id = 'testingId';
newDiv.id = id;
foolEl.appendChild(newDiv);

ReactDOM.render(
<FooBarComponent />
document.getElementById(id)
);


If so is there any other way I can approach this? Basically i'm trying to insert a component into a DOM element.










share|improve this question














So I never really do this but I might have to for a certain problem I'm facing with a 3rd party library.



Basically, Is it bad to do something like



const fooEl = document.querySelector('.foo-class');
const newDiv = document.createElement('div');
const id = 'testingId';
newDiv.id = id;
foolEl.appendChild(newDiv);

ReactDOM.render(
<FooBarComponent />
document.getElementById(id)
);


If so is there any other way I can approach this? Basically i'm trying to insert a component into a DOM element.







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 22:01









christiancn7christiancn7

456 bronze badges




456 bronze badges










  • 1





    What's the problem you're trying to solve?

    – djfdev
    Mar 27 at 22:04











  • This is perfectly valid code. Opinions about whether it's good or bad are off-topic for stack overflow

    – Fred Stark
    Mar 27 at 22:10











  • @FredStark, I think there is a difference between asking for a "best" practice (i.e an opinionated view of which method is better than any other method) and asking if an approach is "bad" (i.e if there are any known side-effects that can be avoided through another method). OP seems to be asking about the latter. The answer here, as you said yourself, is no - there's nothing inherently wrong with this code... but that doesn't make it opinionated.

    – Chris
    Mar 27 at 22:23












  • 1





    What's the problem you're trying to solve?

    – djfdev
    Mar 27 at 22:04











  • This is perfectly valid code. Opinions about whether it's good or bad are off-topic for stack overflow

    – Fred Stark
    Mar 27 at 22:10











  • @FredStark, I think there is a difference between asking for a "best" practice (i.e an opinionated view of which method is better than any other method) and asking if an approach is "bad" (i.e if there are any known side-effects that can be avoided through another method). OP seems to be asking about the latter. The answer here, as you said yourself, is no - there's nothing inherently wrong with this code... but that doesn't make it opinionated.

    – Chris
    Mar 27 at 22:23







1




1





What's the problem you're trying to solve?

– djfdev
Mar 27 at 22:04





What's the problem you're trying to solve?

– djfdev
Mar 27 at 22:04













This is perfectly valid code. Opinions about whether it's good or bad are off-topic for stack overflow

– Fred Stark
Mar 27 at 22:10





This is perfectly valid code. Opinions about whether it's good or bad are off-topic for stack overflow

– Fred Stark
Mar 27 at 22:10













@FredStark, I think there is a difference between asking for a "best" practice (i.e an opinionated view of which method is better than any other method) and asking if an approach is "bad" (i.e if there are any known side-effects that can be avoided through another method). OP seems to be asking about the latter. The answer here, as you said yourself, is no - there's nothing inherently wrong with this code... but that doesn't make it opinionated.

– Chris
Mar 27 at 22:23





@FredStark, I think there is a difference between asking for a "best" practice (i.e an opinionated view of which method is better than any other method) and asking if an approach is "bad" (i.e if there are any known side-effects that can be avoided through another method). OP seems to be asking about the latter. The answer here, as you said yourself, is no - there's nothing inherently wrong with this code... but that doesn't make it opinionated.

– Chris
Mar 27 at 22:23












2 Answers
2






active

oldest

votes


















0















It's the correct practice, you can see that in react documentation:



enter image description here



Another altenative is centralize that in an object apart.






share|improve this answer
































    0
















    Is it bad to do something like...




    No, the code you have there is fine, except that you forgot a , in your ReactDOM.render().
    Other than that, there is nothing "bad" about it. You could achieve the same result with less lines if you instead did:



    const fooEl = document.querySelector('.foo-class');
    const newDiv = document.createElement('div');
    foolEl.appendChild(newDiv);

    ReactDOM.render(
    <FooBarComponent />,
    newDiv
    );


    However, it is not clear from your question why you need to mount the React app to a child div of .foo-class, rather than just mounting it on foo-class directly - like so:



    ReactDOM.render(
    <FooBarComponent />,
    document.querySelector('.foo-class')
    );





    share|improve this answer



























    • I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

      – Chris
      Mar 27 at 22:12











    • I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

      – Keith
      Mar 27 at 23:37











    • @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

      – Chris
      Mar 27 at 23:43














    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387140%2freact-bad-practice-to-use-reactdom-render-and-inject-component%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    It's the correct practice, you can see that in react documentation:



    enter image description here



    Another altenative is centralize that in an object apart.






    share|improve this answer





























      0















      It's the correct practice, you can see that in react documentation:



      enter image description here



      Another altenative is centralize that in an object apart.






      share|improve this answer



























        0














        0










        0









        It's the correct practice, you can see that in react documentation:



        enter image description here



        Another altenative is centralize that in an object apart.






        share|improve this answer













        It's the correct practice, you can see that in react documentation:



        enter image description here



        Another altenative is centralize that in an object apart.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 22:12









        Marco FioritoMarco Fiorito

        295 bronze badges




        295 bronze badges


























            0
















            Is it bad to do something like...




            No, the code you have there is fine, except that you forgot a , in your ReactDOM.render().
            Other than that, there is nothing "bad" about it. You could achieve the same result with less lines if you instead did:



            const fooEl = document.querySelector('.foo-class');
            const newDiv = document.createElement('div');
            foolEl.appendChild(newDiv);

            ReactDOM.render(
            <FooBarComponent />,
            newDiv
            );


            However, it is not clear from your question why you need to mount the React app to a child div of .foo-class, rather than just mounting it on foo-class directly - like so:



            ReactDOM.render(
            <FooBarComponent />,
            document.querySelector('.foo-class')
            );





            share|improve this answer



























            • I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

              – Chris
              Mar 27 at 22:12











            • I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

              – Keith
              Mar 27 at 23:37











            • @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

              – Chris
              Mar 27 at 23:43
















            0
















            Is it bad to do something like...




            No, the code you have there is fine, except that you forgot a , in your ReactDOM.render().
            Other than that, there is nothing "bad" about it. You could achieve the same result with less lines if you instead did:



            const fooEl = document.querySelector('.foo-class');
            const newDiv = document.createElement('div');
            foolEl.appendChild(newDiv);

            ReactDOM.render(
            <FooBarComponent />,
            newDiv
            );


            However, it is not clear from your question why you need to mount the React app to a child div of .foo-class, rather than just mounting it on foo-class directly - like so:



            ReactDOM.render(
            <FooBarComponent />,
            document.querySelector('.foo-class')
            );





            share|improve this answer



























            • I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

              – Chris
              Mar 27 at 22:12











            • I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

              – Keith
              Mar 27 at 23:37











            • @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

              – Chris
              Mar 27 at 23:43














            0














            0










            0










            Is it bad to do something like...




            No, the code you have there is fine, except that you forgot a , in your ReactDOM.render().
            Other than that, there is nothing "bad" about it. You could achieve the same result with less lines if you instead did:



            const fooEl = document.querySelector('.foo-class');
            const newDiv = document.createElement('div');
            foolEl.appendChild(newDiv);

            ReactDOM.render(
            <FooBarComponent />,
            newDiv
            );


            However, it is not clear from your question why you need to mount the React app to a child div of .foo-class, rather than just mounting it on foo-class directly - like so:



            ReactDOM.render(
            <FooBarComponent />,
            document.querySelector('.foo-class')
            );





            share|improve this answer
















            Is it bad to do something like...




            No, the code you have there is fine, except that you forgot a , in your ReactDOM.render().
            Other than that, there is nothing "bad" about it. You could achieve the same result with less lines if you instead did:



            const fooEl = document.querySelector('.foo-class');
            const newDiv = document.createElement('div');
            foolEl.appendChild(newDiv);

            ReactDOM.render(
            <FooBarComponent />,
            newDiv
            );


            However, it is not clear from your question why you need to mount the React app to a child div of .foo-class, rather than just mounting it on foo-class directly - like so:



            ReactDOM.render(
            <FooBarComponent />,
            document.querySelector('.foo-class')
            );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 27 at 23:44

























            answered Mar 27 at 22:10









            ChrisChris

            34.1k12 gold badges69 silver badges92 bronze badges




            34.1k12 gold badges69 silver badges92 bronze badges















            • I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

              – Chris
              Mar 27 at 22:12











            • I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

              – Keith
              Mar 27 at 23:37











            • @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

              – Chris
              Mar 27 at 23:43


















            • I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

              – Chris
              Mar 27 at 22:12











            • I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

              – Keith
              Mar 27 at 23:37











            • @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

              – Chris
              Mar 27 at 23:43

















            I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

            – Chris
            Mar 27 at 22:12





            I'd be very happy to know why this was down-voted. Is there something I missed or that I can improve? Please let me know, because the reason is unclear to me.

            – Chris
            Mar 27 at 22:12













            I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

            – Keith
            Mar 27 at 23:37





            I never down-voted, but have spotted an oddity in your first snippet. document.getElementById(newDiv), well newDiv is already a DOM element, and doing getElementById on it doesn't make any sense.

            – Keith
            Mar 27 at 23:37













            @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

            – Chris
            Mar 27 at 23:43






            @Keith, Well spotted. A hasty copy-paste error on my part :) Fixed now. Thanks!

            – Chris
            Mar 27 at 23:43


















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387140%2freact-bad-practice-to-use-reactdom-render-and-inject-component%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해