How to generate embedded code in ReactJS?











up vote
2
down vote

favorite












I want to generate an embedded code from my ReactJS application. I really don't know how can I achieve such functionality?



for example, I want to generate a code which can be used in another website using the iframe, same like youtube embedded code.



Thanks in advance.










share|improve this question




















  • 1




    Why doesn't this have the javascript tag? reactjs is not a standalone tag
    – Electrox Mortem
    Nov 9 at 4:37










  • yeah! forgot to add. Thanks!
    – Jagdeesh Kumar
    Nov 9 at 4:48















up vote
2
down vote

favorite












I want to generate an embedded code from my ReactJS application. I really don't know how can I achieve such functionality?



for example, I want to generate a code which can be used in another website using the iframe, same like youtube embedded code.



Thanks in advance.










share|improve this question




















  • 1




    Why doesn't this have the javascript tag? reactjs is not a standalone tag
    – Electrox Mortem
    Nov 9 at 4:37










  • yeah! forgot to add. Thanks!
    – Jagdeesh Kumar
    Nov 9 at 4:48













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to generate an embedded code from my ReactJS application. I really don't know how can I achieve such functionality?



for example, I want to generate a code which can be used in another website using the iframe, same like youtube embedded code.



Thanks in advance.










share|improve this question















I want to generate an embedded code from my ReactJS application. I really don't know how can I achieve such functionality?



for example, I want to generate a code which can be used in another website using the iframe, same like youtube embedded code.



Thanks in advance.







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 4:47

























asked Nov 9 at 4:28









Jagdeesh Kumar

383116




383116








  • 1




    Why doesn't this have the javascript tag? reactjs is not a standalone tag
    – Electrox Mortem
    Nov 9 at 4:37










  • yeah! forgot to add. Thanks!
    – Jagdeesh Kumar
    Nov 9 at 4:48














  • 1




    Why doesn't this have the javascript tag? reactjs is not a standalone tag
    – Electrox Mortem
    Nov 9 at 4:37










  • yeah! forgot to add. Thanks!
    – Jagdeesh Kumar
    Nov 9 at 4:48








1




1




Why doesn't this have the javascript tag? reactjs is not a standalone tag
– Electrox Mortem
Nov 9 at 4:37




Why doesn't this have the javascript tag? reactjs is not a standalone tag
– Electrox Mortem
Nov 9 at 4:37












yeah! forgot to add. Thanks!
– Jagdeesh Kumar
Nov 9 at 4:48




yeah! forgot to add. Thanks!
– Jagdeesh Kumar
Nov 9 at 4:48












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can do that with below steps.



1.Add DOM element container to contain your component



<!-- ... Your HTML code ... -->

<div id="example_container"></div>

<!-- ...Your HTML code ... -->


The div tag will help us to find out this element from our script code to render our desired react code within it.
2. Add your react script.Please make sure you load react before your script.



<!-- Load react scripts -->
<!-- Load our React component. -->
<script src="example.js"></script>


3. After your code in example.js which has ExampleReactClass extends React.Componentand const e = React.createElement; add the below lines to fix your code in mentioned <div>



const domContainer = document.querySelector('#example_container');
ReactDOM.render(e(ExampleReactClass), domContainer);


Hope it will work!!






share|improve this answer























  • thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
    – Jagdeesh Kumar
    Nov 9 at 5:18








  • 1




    Please do find the updated edit for better clarity for other parameters :)
    – Aman sharma
    Nov 9 at 5:26


















up vote
0
down vote













Since the question has example of youtube, lets start with that.



<iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>


Our main concern is the src attribute, rest of the iframe attributes can be considered if needed.



To make things easier we need to have Router to handle the embed url request from iframe



import { BrowserRouter } from 'react-router-dom'

ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('root'))


Add a route to handle /embed/h04NnnUApaM



<Route path='/embed/:id' component={YourComponent}/>


Get the embed id in YourComponent like this props.match.params.number



render the component, and it should show in the iframe.



Generate embed code



Depends on what you want to embed.



<textarea>{this.state.embedCode}<textarea>


embedCode will be something like this for the above example (id depends on what you need to render your component from the embeded page)



<iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM"></iframe>





share|improve this answer





















    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',
    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%2f53219882%2fhow-to-generate-embedded-code-in-reactjs%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








    up vote
    0
    down vote













    You can do that with below steps.



    1.Add DOM element container to contain your component



    <!-- ... Your HTML code ... -->

    <div id="example_container"></div>

    <!-- ...Your HTML code ... -->


    The div tag will help us to find out this element from our script code to render our desired react code within it.
    2. Add your react script.Please make sure you load react before your script.



    <!-- Load react scripts -->
    <!-- Load our React component. -->
    <script src="example.js"></script>


    3. After your code in example.js which has ExampleReactClass extends React.Componentand const e = React.createElement; add the below lines to fix your code in mentioned <div>



    const domContainer = document.querySelector('#example_container');
    ReactDOM.render(e(ExampleReactClass), domContainer);


    Hope it will work!!






    share|improve this answer























    • thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
      – Jagdeesh Kumar
      Nov 9 at 5:18








    • 1




      Please do find the updated edit for better clarity for other parameters :)
      – Aman sharma
      Nov 9 at 5:26















    up vote
    0
    down vote













    You can do that with below steps.



    1.Add DOM element container to contain your component



    <!-- ... Your HTML code ... -->

    <div id="example_container"></div>

    <!-- ...Your HTML code ... -->


    The div tag will help us to find out this element from our script code to render our desired react code within it.
    2. Add your react script.Please make sure you load react before your script.



    <!-- Load react scripts -->
    <!-- Load our React component. -->
    <script src="example.js"></script>


    3. After your code in example.js which has ExampleReactClass extends React.Componentand const e = React.createElement; add the below lines to fix your code in mentioned <div>



    const domContainer = document.querySelector('#example_container');
    ReactDOM.render(e(ExampleReactClass), domContainer);


    Hope it will work!!






    share|improve this answer























    • thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
      – Jagdeesh Kumar
      Nov 9 at 5:18








    • 1




      Please do find the updated edit for better clarity for other parameters :)
      – Aman sharma
      Nov 9 at 5:26













    up vote
    0
    down vote










    up vote
    0
    down vote









    You can do that with below steps.



    1.Add DOM element container to contain your component



    <!-- ... Your HTML code ... -->

    <div id="example_container"></div>

    <!-- ...Your HTML code ... -->


    The div tag will help us to find out this element from our script code to render our desired react code within it.
    2. Add your react script.Please make sure you load react before your script.



    <!-- Load react scripts -->
    <!-- Load our React component. -->
    <script src="example.js"></script>


    3. After your code in example.js which has ExampleReactClass extends React.Componentand const e = React.createElement; add the below lines to fix your code in mentioned <div>



    const domContainer = document.querySelector('#example_container');
    ReactDOM.render(e(ExampleReactClass), domContainer);


    Hope it will work!!






    share|improve this answer














    You can do that with below steps.



    1.Add DOM element container to contain your component



    <!-- ... Your HTML code ... -->

    <div id="example_container"></div>

    <!-- ...Your HTML code ... -->


    The div tag will help us to find out this element from our script code to render our desired react code within it.
    2. Add your react script.Please make sure you load react before your script.



    <!-- Load react scripts -->
    <!-- Load our React component. -->
    <script src="example.js"></script>


    3. After your code in example.js which has ExampleReactClass extends React.Componentand const e = React.createElement; add the below lines to fix your code in mentioned <div>



    const domContainer = document.querySelector('#example_container');
    ReactDOM.render(e(ExampleReactClass), domContainer);


    Hope it will work!!







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 5:25

























    answered Nov 9 at 5:12









    Aman sharma

    776




    776












    • thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
      – Jagdeesh Kumar
      Nov 9 at 5:18








    • 1




      Please do find the updated edit for better clarity for other parameters :)
      – Aman sharma
      Nov 9 at 5:26


















    • thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
      – Jagdeesh Kumar
      Nov 9 at 5:18








    • 1




      Please do find the updated edit for better clarity for other parameters :)
      – Aman sharma
      Nov 9 at 5:26
















    thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
    – Jagdeesh Kumar
    Nov 9 at 5:18






    thanks for your comment. let me try if it works for me :). I will not forget to accept and upvote your answer.
    – Jagdeesh Kumar
    Nov 9 at 5:18






    1




    1




    Please do find the updated edit for better clarity for other parameters :)
    – Aman sharma
    Nov 9 at 5:26




    Please do find the updated edit for better clarity for other parameters :)
    – Aman sharma
    Nov 9 at 5:26












    up vote
    0
    down vote













    Since the question has example of youtube, lets start with that.



    <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>


    Our main concern is the src attribute, rest of the iframe attributes can be considered if needed.



    To make things easier we need to have Router to handle the embed url request from iframe



    import { BrowserRouter } from 'react-router-dom'

    ReactDOM.render((
    <BrowserRouter>
    <App />
    </BrowserRouter>
    ), document.getElementById('root'))


    Add a route to handle /embed/h04NnnUApaM



    <Route path='/embed/:id' component={YourComponent}/>


    Get the embed id in YourComponent like this props.match.params.number



    render the component, and it should show in the iframe.



    Generate embed code



    Depends on what you want to embed.



    <textarea>{this.state.embedCode}<textarea>


    embedCode will be something like this for the above example (id depends on what you need to render your component from the embeded page)



    <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM"></iframe>





    share|improve this answer

























      up vote
      0
      down vote













      Since the question has example of youtube, lets start with that.



      <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>


      Our main concern is the src attribute, rest of the iframe attributes can be considered if needed.



      To make things easier we need to have Router to handle the embed url request from iframe



      import { BrowserRouter } from 'react-router-dom'

      ReactDOM.render((
      <BrowserRouter>
      <App />
      </BrowserRouter>
      ), document.getElementById('root'))


      Add a route to handle /embed/h04NnnUApaM



      <Route path='/embed/:id' component={YourComponent}/>


      Get the embed id in YourComponent like this props.match.params.number



      render the component, and it should show in the iframe.



      Generate embed code



      Depends on what you want to embed.



      <textarea>{this.state.embedCode}<textarea>


      embedCode will be something like this for the above example (id depends on what you need to render your component from the embeded page)



      <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM"></iframe>





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Since the question has example of youtube, lets start with that.



        <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>


        Our main concern is the src attribute, rest of the iframe attributes can be considered if needed.



        To make things easier we need to have Router to handle the embed url request from iframe



        import { BrowserRouter } from 'react-router-dom'

        ReactDOM.render((
        <BrowserRouter>
        <App />
        </BrowserRouter>
        ), document.getElementById('root'))


        Add a route to handle /embed/h04NnnUApaM



        <Route path='/embed/:id' component={YourComponent}/>


        Get the embed id in YourComponent like this props.match.params.number



        render the component, and it should show in the iframe.



        Generate embed code



        Depends on what you want to embed.



        <textarea>{this.state.embedCode}<textarea>


        embedCode will be something like this for the above example (id depends on what you need to render your component from the embeded page)



        <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM"></iframe>





        share|improve this answer












        Since the question has example of youtube, lets start with that.



        <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>


        Our main concern is the src attribute, rest of the iframe attributes can be considered if needed.



        To make things easier we need to have Router to handle the embed url request from iframe



        import { BrowserRouter } from 'react-router-dom'

        ReactDOM.render((
        <BrowserRouter>
        <App />
        </BrowserRouter>
        ), document.getElementById('root'))


        Add a route to handle /embed/h04NnnUApaM



        <Route path='/embed/:id' component={YourComponent}/>


        Get the embed id in YourComponent like this props.match.params.number



        render the component, and it should show in the iframe.



        Generate embed code



        Depends on what you want to embed.



        <textarea>{this.state.embedCode}<textarea>


        embedCode will be something like this for the above example (id depends on what you need to render your component from the embeded page)



        <iframe width="560" height="315" src="https://www.yoursite.com/embed/h04NnnUApaM"></iframe>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 5:48









        kiranvj

        12.1k23350




        12.1k23350






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53219882%2fhow-to-generate-embedded-code-in-reactjs%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini