how can injection dynamic html element to page with next.js?how injection function with dangerouslySetInnerHTMLHow can I update NodeJS and NPM to the next versions?How can I get the full object in Node.js's console.log(), rather than '[Object]'?Uncompress gzipped http request body to json in Node.jsHow can I update npm on Windows?jest, enzyme - testing a method that returns jsxHow to create subpages in NEXT.jsHow to build next.js production?NextJS multiple fetches paralellNext.js - how to set head tags dynamicallyNext.js dynamic page params for static export

How did medieval manors handle population growth? Was there room for more fields to be ploughed?

Was it illegal to blaspheme God in Antioch in 360.-410.?

Can two aircraft be allowed to stay on the same runway at the same time?

Lob Logical Read and lob read-ahead reads in NCCI

How were US credit cards verified in-store in the 1980's?

Rapid change in character

Ask one verbal question to figure out who is blind and who is mute among three persons

Could a complex system of reaction wheels be used to propel a spacecraft?

Can inductive kick be discharged without freewheeling diode, in this example?

What do the different role icons backgrounds mean?

What is the motivation behind designing a control stick that does not move?

Resources to learn about firearms?

Can UV radiation be safe for the skin?

Can I leave a large suitcase at TPE during a 4-hour layover, and pick it up 4.5 days later when I come back to TPE on my way to Taipei downtown?

Are sweatpants frowned upon on flights?

'Horseshoes' for Deer?

IList<T> implementation

Magnetic thread storage?

Create a list of snaking numbers under 50,000

Why do presidential pardons exist in a country having a clear separation of powers?

What is this "opened" cube called?

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

Was a six-engine 747 ever seriously considered by Boeing?

Sum and average calculator



how can injection dynamic html element to page with next.js?


how injection function with dangerouslySetInnerHTMLHow can I update NodeJS and NPM to the next versions?How can I get the full object in Node.js's console.log(), rather than '[Object]'?Uncompress gzipped http request body to json in Node.jsHow can I update npm on Windows?jest, enzyme - testing a method that returns jsxHow to create subpages in NEXT.jsHow to build next.js production?NextJS multiple fetches paralellNext.js - how to set head tags dynamicallyNext.js dynamic page params for static export






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








0















how can dynamic injection html element to page with next.js? that these elements Unknown type like(input, checkbox, img,...). this element specified with api that return json type like this:



[
"id":"rooms",
"title":"Rooms",
"order":1,
"type":"string",
"widget":"select",
"data":[
"Id":18,
"ParentId":null,
"Title":"One",
"Level":null,
"Childrens":[]
,
"Id":19,
"ParentId":null,
"Title":"Two",
"Level":null,
"Childrens":[]
,
"Id":20,
"ParentId":null,
"Title":"Three",
"Level":null,
"Childrens":[]
]
,

"id":"exchange",
"title":"Exchange",
"order":0,
"type":"boolean",
"widget":"checkbox",
"data":[]
]


my try is:



Index.getInitialProps = async function(req, query) 

const res= await fetch('url api')
var elements= await res.json()

var test = () => (
<div>
...... convert json to html elements.......
</div>
)

return
test

)


function Index(props) 
return(
<a>
props.test
</a>
)



result is null, mean nothing for presentation.



the question is, Do I do the right thing? Is there a better way?










share|improve this question
































    0















    how can dynamic injection html element to page with next.js? that these elements Unknown type like(input, checkbox, img,...). this element specified with api that return json type like this:



    [
    "id":"rooms",
    "title":"Rooms",
    "order":1,
    "type":"string",
    "widget":"select",
    "data":[
    "Id":18,
    "ParentId":null,
    "Title":"One",
    "Level":null,
    "Childrens":[]
    ,
    "Id":19,
    "ParentId":null,
    "Title":"Two",
    "Level":null,
    "Childrens":[]
    ,
    "Id":20,
    "ParentId":null,
    "Title":"Three",
    "Level":null,
    "Childrens":[]
    ]
    ,

    "id":"exchange",
    "title":"Exchange",
    "order":0,
    "type":"boolean",
    "widget":"checkbox",
    "data":[]
    ]


    my try is:



    Index.getInitialProps = async function(req, query) 

    const res= await fetch('url api')
    var elements= await res.json()

    var test = () => (
    <div>
    ...... convert json to html elements.......
    </div>
    )

    return
    test

    )


    function Index(props) 
    return(
    <a>
    props.test
    </a>
    )



    result is null, mean nothing for presentation.



    the question is, Do I do the right thing? Is there a better way?










    share|improve this question




























      0












      0








      0








      how can dynamic injection html element to page with next.js? that these elements Unknown type like(input, checkbox, img,...). this element specified with api that return json type like this:



      [
      "id":"rooms",
      "title":"Rooms",
      "order":1,
      "type":"string",
      "widget":"select",
      "data":[
      "Id":18,
      "ParentId":null,
      "Title":"One",
      "Level":null,
      "Childrens":[]
      ,
      "Id":19,
      "ParentId":null,
      "Title":"Two",
      "Level":null,
      "Childrens":[]
      ,
      "Id":20,
      "ParentId":null,
      "Title":"Three",
      "Level":null,
      "Childrens":[]
      ]
      ,

      "id":"exchange",
      "title":"Exchange",
      "order":0,
      "type":"boolean",
      "widget":"checkbox",
      "data":[]
      ]


      my try is:



      Index.getInitialProps = async function(req, query) 

      const res= await fetch('url api')
      var elements= await res.json()

      var test = () => (
      <div>
      ...... convert json to html elements.......
      </div>
      )

      return
      test

      )


      function Index(props) 
      return(
      <a>
      props.test
      </a>
      )



      result is null, mean nothing for presentation.



      the question is, Do I do the right thing? Is there a better way?










      share|improve this question
















      how can dynamic injection html element to page with next.js? that these elements Unknown type like(input, checkbox, img,...). this element specified with api that return json type like this:



      [
      "id":"rooms",
      "title":"Rooms",
      "order":1,
      "type":"string",
      "widget":"select",
      "data":[
      "Id":18,
      "ParentId":null,
      "Title":"One",
      "Level":null,
      "Childrens":[]
      ,
      "Id":19,
      "ParentId":null,
      "Title":"Two",
      "Level":null,
      "Childrens":[]
      ,
      "Id":20,
      "ParentId":null,
      "Title":"Three",
      "Level":null,
      "Childrens":[]
      ]
      ,

      "id":"exchange",
      "title":"Exchange",
      "order":0,
      "type":"boolean",
      "widget":"checkbox",
      "data":[]
      ]


      my try is:



      Index.getInitialProps = async function(req, query) 

      const res= await fetch('url api')
      var elements= await res.json()

      var test = () => (
      <div>
      ...... convert json to html elements.......
      </div>
      )

      return
      test

      )


      function Index(props) 
      return(
      <a>
      props.test
      </a>
      )



      result is null, mean nothing for presentation.



      the question is, Do I do the right thing? Is there a better way?







      node.js reactjs next.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 6:41







      Hooshyar Qaderi

















      asked Mar 27 at 23:01









      Hooshyar QaderiHooshyar Qaderi

      84 bronze badges




      84 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0















          What happens is that during the transfer of props from server to client in getInitialprops, JSON is serialized and so functions are not really serialized. See https://github.com/zeit/next.js/issues/3536



          Your best bet is to convert the test data into a string of HTML data and inject it using dangerouslySetInnerHTML. An example will be:



          class TestComponent extends React.Component 
          static async getInitialProps()
          const text = '<div class="homepsage">This is the homepage data</div>';
          return text ;


          render()
          return (
          <div>
          <div className="text-container" dangerouslySetInnerHTML= __html: this.props.text />
          <h1>Hello world</div>
          </div>
          );




          The catch with this is that the string you return must be a valid HTML (not JSX). So notice I used class instead of className



          You can read more about it here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml






          share|improve this answer

























          • thanks It was very useful. But, Is this a usual injection method?

            – Hooshyar Qaderi
            Mar 28 at 9:26











          • I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

            – cr05s19xx
            Mar 28 at 11:31










          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%2f55387778%2fhow-can-injection-dynamic-html-element-to-page-with-next-js%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          What happens is that during the transfer of props from server to client in getInitialprops, JSON is serialized and so functions are not really serialized. See https://github.com/zeit/next.js/issues/3536



          Your best bet is to convert the test data into a string of HTML data and inject it using dangerouslySetInnerHTML. An example will be:



          class TestComponent extends React.Component 
          static async getInitialProps()
          const text = '<div class="homepsage">This is the homepage data</div>';
          return text ;


          render()
          return (
          <div>
          <div className="text-container" dangerouslySetInnerHTML= __html: this.props.text />
          <h1>Hello world</div>
          </div>
          );




          The catch with this is that the string you return must be a valid HTML (not JSX). So notice I used class instead of className



          You can read more about it here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml






          share|improve this answer

























          • thanks It was very useful. But, Is this a usual injection method?

            – Hooshyar Qaderi
            Mar 28 at 9:26











          • I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

            – cr05s19xx
            Mar 28 at 11:31















          0















          What happens is that during the transfer of props from server to client in getInitialprops, JSON is serialized and so functions are not really serialized. See https://github.com/zeit/next.js/issues/3536



          Your best bet is to convert the test data into a string of HTML data and inject it using dangerouslySetInnerHTML. An example will be:



          class TestComponent extends React.Component 
          static async getInitialProps()
          const text = '<div class="homepsage">This is the homepage data</div>';
          return text ;


          render()
          return (
          <div>
          <div className="text-container" dangerouslySetInnerHTML= __html: this.props.text />
          <h1>Hello world</div>
          </div>
          );




          The catch with this is that the string you return must be a valid HTML (not JSX). So notice I used class instead of className



          You can read more about it here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml






          share|improve this answer

























          • thanks It was very useful. But, Is this a usual injection method?

            – Hooshyar Qaderi
            Mar 28 at 9:26











          • I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

            – cr05s19xx
            Mar 28 at 11:31













          0














          0










          0









          What happens is that during the transfer of props from server to client in getInitialprops, JSON is serialized and so functions are not really serialized. See https://github.com/zeit/next.js/issues/3536



          Your best bet is to convert the test data into a string of HTML data and inject it using dangerouslySetInnerHTML. An example will be:



          class TestComponent extends React.Component 
          static async getInitialProps()
          const text = '<div class="homepsage">This is the homepage data</div>';
          return text ;


          render()
          return (
          <div>
          <div className="text-container" dangerouslySetInnerHTML= __html: this.props.text />
          <h1>Hello world</div>
          </div>
          );




          The catch with this is that the string you return must be a valid HTML (not JSX). So notice I used class instead of className



          You can read more about it here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml






          share|improve this answer













          What happens is that during the transfer of props from server to client in getInitialprops, JSON is serialized and so functions are not really serialized. See https://github.com/zeit/next.js/issues/3536



          Your best bet is to convert the test data into a string of HTML data and inject it using dangerouslySetInnerHTML. An example will be:



          class TestComponent extends React.Component 
          static async getInitialProps()
          const text = '<div class="homepsage">This is the homepage data</div>';
          return text ;


          render()
          return (
          <div>
          <div className="text-container" dangerouslySetInnerHTML= __html: this.props.text />
          <h1>Hello world</div>
          </div>
          );




          The catch with this is that the string you return must be a valid HTML (not JSX). So notice I used class instead of className



          You can read more about it here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 at 8:45









          cr05s19xxcr05s19xx

          6677 silver badges22 bronze badges




          6677 silver badges22 bronze badges















          • thanks It was very useful. But, Is this a usual injection method?

            – Hooshyar Qaderi
            Mar 28 at 9:26











          • I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

            – cr05s19xx
            Mar 28 at 11:31

















          • thanks It was very useful. But, Is this a usual injection method?

            – Hooshyar Qaderi
            Mar 28 at 9:26











          • I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

            – cr05s19xx
            Mar 28 at 11:31
















          thanks It was very useful. But, Is this a usual injection method?

          – Hooshyar Qaderi
          Mar 28 at 9:26





          thanks It was very useful. But, Is this a usual injection method?

          – Hooshyar Qaderi
          Mar 28 at 9:26













          I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

          – cr05s19xx
          Mar 28 at 11:31





          I won't say it is the go-to standard. There really isn't a goto standard. You could also return the json data and parse that into real JSX if you want

          – cr05s19xx
          Mar 28 at 11:31








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55387778%2fhow-can-injection-dynamic-html-element-to-page-with-next-js%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript