React pagination without JSX

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





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







up vote
3
down vote

favorite












I've published my first react project for pagination purposes. It's likely I'm making mistakes or there are any improvements to my code. I'd appreciate if anyone has any offers.



jQuery(document).ready(function($)

/*
Simple project to implementing pagination with React
Developer: Alex Jolig

Licence:MIT
Source:https://github.com/alexjolig/react-pagination-without-jsx
Contact me if you need to: alex.jolig@gmail.com

*/

class ImageGallery extends React.Component
constructor(props)
super(props);

this.state =
posts: , //However its not relevant in this sample but I named the array posts. feel free to name it as you wish
currentPage: 1,
postsPerPage: 5
;
this.handleClick = this.handleClick.bind(this);
this.changePage = this.changePage.bind(this);


//put AJAX request in componentDidMount() method
componentDidMount()

$.ajax(
url: "generated.json",
dataType: 'json',
cache: false,
success: function(data)
this.setState(posts: data);
console.log(data);
.bind(this),
error: function(xhr, status, err)
console.error("", status, err.toString());
.bind(this)
);


//Handle click on page number list
handleClick(event)
this.setState(
currentPage: Number(event.currentTarget.id) //using event.target would reference to child tag (a). So use event.currentTarget
);


//Handle next and previous buttons
changePage(event)
let newPage = 1;
if(event.currentTarget.id === 'p-page')
newPage = this.state.currentPage > 1 ? this.state.currentPage - 1 : 1;
this.setState(
currentPage: newPage
);

else
let pageCount = this.state.posts.length / this.state.postsPerPage;
newPage = this.state.currentPage < pageCount ? this.state.currentPage + 1 : pageCount;
this.setState(
currentPage: newPage
);



render()

const posts, currentPage, postsPerPage = this.state;

// Logic for displaying current posts
const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

const renderPosts = currentPosts.map((post, index) =>
return React.createElement('li', key: post.index,
React.createElement('h2', , post.name),
React.createElement('a', href: post.phone, post.phone)
)
);

// Logic for displaying page numbers
const pageNumbers = ;
for (let i = 1; i <= Math.ceil(posts.length / postsPerPage); i++)
pageNumbers.push(i);


const prevPage = React.createElement(
'li', className: 'page-item',
key:'p-page',
id: 'p-page',
onClick: this.changePage
,
React.createElement('a', className: 'page-link',
href: '#'
, 'Previous'
)
);

const nextPage = React.createElement(
'li', className: 'page-item',
key:'n-page',
id: 'n-page',
onClick: this.changePage
,
React.createElement('a', className: 'page-link',
href: '#'
, 'Next'
)
);

const renderPageNumbers = pageNumbers.map(number =>
return React.createElement(
'li', className: 'page-item' + (currentPage === number ? ' active' : '') ,
key:number,
id: number,
onClick: this.handleClick
,
React.createElement('a', className: 'page-link',
href: '#'
, number
)
);
);

//NOTE: return elements as array is added in react 16+
return [
React.createElement('div', key: 'a', className: 'row',
React.createElement('ul', key:'a', renderPosts)
),
React.createElement('div', key: 'b', className: 'row',
React.createElement('ul', className: 'pagination',key: 'b',
prevPage,
renderPageNumbers,
nextPage)
)
];



ReactDOM.render(
React.createElement(ImageGallery, null),
document.getElementById('root')
);
);






share|improve this question



























    up vote
    3
    down vote

    favorite












    I've published my first react project for pagination purposes. It's likely I'm making mistakes or there are any improvements to my code. I'd appreciate if anyone has any offers.



    jQuery(document).ready(function($)

    /*
    Simple project to implementing pagination with React
    Developer: Alex Jolig

    Licence:MIT
    Source:https://github.com/alexjolig/react-pagination-without-jsx
    Contact me if you need to: alex.jolig@gmail.com

    */

    class ImageGallery extends React.Component
    constructor(props)
    super(props);

    this.state =
    posts: , //However its not relevant in this sample but I named the array posts. feel free to name it as you wish
    currentPage: 1,
    postsPerPage: 5
    ;
    this.handleClick = this.handleClick.bind(this);
    this.changePage = this.changePage.bind(this);


    //put AJAX request in componentDidMount() method
    componentDidMount()

    $.ajax(
    url: "generated.json",
    dataType: 'json',
    cache: false,
    success: function(data)
    this.setState(posts: data);
    console.log(data);
    .bind(this),
    error: function(xhr, status, err)
    console.error("", status, err.toString());
    .bind(this)
    );


    //Handle click on page number list
    handleClick(event)
    this.setState(
    currentPage: Number(event.currentTarget.id) //using event.target would reference to child tag (a). So use event.currentTarget
    );


    //Handle next and previous buttons
    changePage(event)
    let newPage = 1;
    if(event.currentTarget.id === 'p-page')
    newPage = this.state.currentPage > 1 ? this.state.currentPage - 1 : 1;
    this.setState(
    currentPage: newPage
    );

    else
    let pageCount = this.state.posts.length / this.state.postsPerPage;
    newPage = this.state.currentPage < pageCount ? this.state.currentPage + 1 : pageCount;
    this.setState(
    currentPage: newPage
    );



    render()

    const posts, currentPage, postsPerPage = this.state;

    // Logic for displaying current posts
    const indexOfLastPost = currentPage * postsPerPage;
    const indexOfFirstPost = indexOfLastPost - postsPerPage;
    const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

    const renderPosts = currentPosts.map((post, index) =>
    return React.createElement('li', key: post.index,
    React.createElement('h2', , post.name),
    React.createElement('a', href: post.phone, post.phone)
    )
    );

    // Logic for displaying page numbers
    const pageNumbers = ;
    for (let i = 1; i <= Math.ceil(posts.length / postsPerPage); i++)
    pageNumbers.push(i);


    const prevPage = React.createElement(
    'li', className: 'page-item',
    key:'p-page',
    id: 'p-page',
    onClick: this.changePage
    ,
    React.createElement('a', className: 'page-link',
    href: '#'
    , 'Previous'
    )
    );

    const nextPage = React.createElement(
    'li', className: 'page-item',
    key:'n-page',
    id: 'n-page',
    onClick: this.changePage
    ,
    React.createElement('a', className: 'page-link',
    href: '#'
    , 'Next'
    )
    );

    const renderPageNumbers = pageNumbers.map(number =>
    return React.createElement(
    'li', className: 'page-item' + (currentPage === number ? ' active' : '') ,
    key:number,
    id: number,
    onClick: this.handleClick
    ,
    React.createElement('a', className: 'page-link',
    href: '#'
    , number
    )
    );
    );

    //NOTE: return elements as array is added in react 16+
    return [
    React.createElement('div', key: 'a', className: 'row',
    React.createElement('ul', key:'a', renderPosts)
    ),
    React.createElement('div', key: 'b', className: 'row',
    React.createElement('ul', className: 'pagination',key: 'b',
    prevPage,
    renderPageNumbers,
    nextPage)
    )
    ];



    ReactDOM.render(
    React.createElement(ImageGallery, null),
    document.getElementById('root')
    );
    );






    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I've published my first react project for pagination purposes. It's likely I'm making mistakes or there are any improvements to my code. I'd appreciate if anyone has any offers.



      jQuery(document).ready(function($)

      /*
      Simple project to implementing pagination with React
      Developer: Alex Jolig

      Licence:MIT
      Source:https://github.com/alexjolig/react-pagination-without-jsx
      Contact me if you need to: alex.jolig@gmail.com

      */

      class ImageGallery extends React.Component
      constructor(props)
      super(props);

      this.state =
      posts: , //However its not relevant in this sample but I named the array posts. feel free to name it as you wish
      currentPage: 1,
      postsPerPage: 5
      ;
      this.handleClick = this.handleClick.bind(this);
      this.changePage = this.changePage.bind(this);


      //put AJAX request in componentDidMount() method
      componentDidMount()

      $.ajax(
      url: "generated.json",
      dataType: 'json',
      cache: false,
      success: function(data)
      this.setState(posts: data);
      console.log(data);
      .bind(this),
      error: function(xhr, status, err)
      console.error("", status, err.toString());
      .bind(this)
      );


      //Handle click on page number list
      handleClick(event)
      this.setState(
      currentPage: Number(event.currentTarget.id) //using event.target would reference to child tag (a). So use event.currentTarget
      );


      //Handle next and previous buttons
      changePage(event)
      let newPage = 1;
      if(event.currentTarget.id === 'p-page')
      newPage = this.state.currentPage > 1 ? this.state.currentPage - 1 : 1;
      this.setState(
      currentPage: newPage
      );

      else
      let pageCount = this.state.posts.length / this.state.postsPerPage;
      newPage = this.state.currentPage < pageCount ? this.state.currentPage + 1 : pageCount;
      this.setState(
      currentPage: newPage
      );



      render()

      const posts, currentPage, postsPerPage = this.state;

      // Logic for displaying current posts
      const indexOfLastPost = currentPage * postsPerPage;
      const indexOfFirstPost = indexOfLastPost - postsPerPage;
      const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

      const renderPosts = currentPosts.map((post, index) =>
      return React.createElement('li', key: post.index,
      React.createElement('h2', , post.name),
      React.createElement('a', href: post.phone, post.phone)
      )
      );

      // Logic for displaying page numbers
      const pageNumbers = ;
      for (let i = 1; i <= Math.ceil(posts.length / postsPerPage); i++)
      pageNumbers.push(i);


      const prevPage = React.createElement(
      'li', className: 'page-item',
      key:'p-page',
      id: 'p-page',
      onClick: this.changePage
      ,
      React.createElement('a', className: 'page-link',
      href: '#'
      , 'Previous'
      )
      );

      const nextPage = React.createElement(
      'li', className: 'page-item',
      key:'n-page',
      id: 'n-page',
      onClick: this.changePage
      ,
      React.createElement('a', className: 'page-link',
      href: '#'
      , 'Next'
      )
      );

      const renderPageNumbers = pageNumbers.map(number =>
      return React.createElement(
      'li', className: 'page-item' + (currentPage === number ? ' active' : '') ,
      key:number,
      id: number,
      onClick: this.handleClick
      ,
      React.createElement('a', className: 'page-link',
      href: '#'
      , number
      )
      );
      );

      //NOTE: return elements as array is added in react 16+
      return [
      React.createElement('div', key: 'a', className: 'row',
      React.createElement('ul', key:'a', renderPosts)
      ),
      React.createElement('div', key: 'b', className: 'row',
      React.createElement('ul', className: 'pagination',key: 'b',
      prevPage,
      renderPageNumbers,
      nextPage)
      )
      ];



      ReactDOM.render(
      React.createElement(ImageGallery, null),
      document.getElementById('root')
      );
      );






      share|improve this question













      I've published my first react project for pagination purposes. It's likely I'm making mistakes or there are any improvements to my code. I'd appreciate if anyone has any offers.



      jQuery(document).ready(function($)

      /*
      Simple project to implementing pagination with React
      Developer: Alex Jolig

      Licence:MIT
      Source:https://github.com/alexjolig/react-pagination-without-jsx
      Contact me if you need to: alex.jolig@gmail.com

      */

      class ImageGallery extends React.Component
      constructor(props)
      super(props);

      this.state =
      posts: , //However its not relevant in this sample but I named the array posts. feel free to name it as you wish
      currentPage: 1,
      postsPerPage: 5
      ;
      this.handleClick = this.handleClick.bind(this);
      this.changePage = this.changePage.bind(this);


      //put AJAX request in componentDidMount() method
      componentDidMount()

      $.ajax(
      url: "generated.json",
      dataType: 'json',
      cache: false,
      success: function(data)
      this.setState(posts: data);
      console.log(data);
      .bind(this),
      error: function(xhr, status, err)
      console.error("", status, err.toString());
      .bind(this)
      );


      //Handle click on page number list
      handleClick(event)
      this.setState(
      currentPage: Number(event.currentTarget.id) //using event.target would reference to child tag (a). So use event.currentTarget
      );


      //Handle next and previous buttons
      changePage(event)
      let newPage = 1;
      if(event.currentTarget.id === 'p-page')
      newPage = this.state.currentPage > 1 ? this.state.currentPage - 1 : 1;
      this.setState(
      currentPage: newPage
      );

      else
      let pageCount = this.state.posts.length / this.state.postsPerPage;
      newPage = this.state.currentPage < pageCount ? this.state.currentPage + 1 : pageCount;
      this.setState(
      currentPage: newPage
      );



      render()

      const posts, currentPage, postsPerPage = this.state;

      // Logic for displaying current posts
      const indexOfLastPost = currentPage * postsPerPage;
      const indexOfFirstPost = indexOfLastPost - postsPerPage;
      const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

      const renderPosts = currentPosts.map((post, index) =>
      return React.createElement('li', key: post.index,
      React.createElement('h2', , post.name),
      React.createElement('a', href: post.phone, post.phone)
      )
      );

      // Logic for displaying page numbers
      const pageNumbers = ;
      for (let i = 1; i <= Math.ceil(posts.length / postsPerPage); i++)
      pageNumbers.push(i);


      const prevPage = React.createElement(
      'li', className: 'page-item',
      key:'p-page',
      id: 'p-page',
      onClick: this.changePage
      ,
      React.createElement('a', className: 'page-link',
      href: '#'
      , 'Previous'
      )
      );

      const nextPage = React.createElement(
      'li', className: 'page-item',
      key:'n-page',
      id: 'n-page',
      onClick: this.changePage
      ,
      React.createElement('a', className: 'page-link',
      href: '#'
      , 'Next'
      )
      );

      const renderPageNumbers = pageNumbers.map(number =>
      return React.createElement(
      'li', className: 'page-item' + (currentPage === number ? ' active' : '') ,
      key:number,
      id: number,
      onClick: this.handleClick
      ,
      React.createElement('a', className: 'page-link',
      href: '#'
      , number
      )
      );
      );

      //NOTE: return elements as array is added in react 16+
      return [
      React.createElement('div', key: 'a', className: 'row',
      React.createElement('ul', key:'a', renderPosts)
      ),
      React.createElement('div', key: 'b', className: 'row',
      React.createElement('ul', className: 'pagination',key: 'b',
      prevPage,
      renderPageNumbers,
      nextPage)
      )
      ];



      ReactDOM.render(
      React.createElement(ImageGallery, null),
      document.getElementById('root')
      );
      );








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 10 at 13:32









      200_success

      123k14143399




      123k14143399









      asked Jun 10 at 8:13









      Alex Jolig

      1184




      1184




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I recommend you to use JSX and build tools because they are totally awesome!



          People use npm and bundlers (like Webpack) because loading each library separately makes the page significantly slower. Let me show you an extreme example. Feel free to take a peek at the source code. And that's not to mention that it's simpler to update my libraries using npm.



          JSX can help you effortlessly construct the element tree in React. You're missing out a lot if you avoid JSX!



          You can create an alias for React.createElement to make your code more readable.



          const h = React.createElement;



          //NOTE: return elements as array is added in react 16+




          You're right. Did you know that React 16 also has a cooler feature?



          If you used Fragments, you wouldn't need to add keys to the two divs you rendered in the render() method.



          Here's the improved code:



          return h(React.Fragment, null,
          h("div", className: "row" ,
          h("ul", null, renderPosts)
          ),
          h("div", className: "row" ,
          h("ul", className: "pagination" ),
          prevPage,
          renderPageNumbers,
          nextPage
          )
          )


          You don't need to pass null as the second parameter here:



          ReactDOM.render(
          React.createElement(ImageGallery, null),
          document.getElementById("root")
          );




          ReactDOM.render(h(ImageGallery), document.getElementById("root"))


          You don't need to use jQuery(document).ready when you've already put your <script> tags under the container element.



          The React community prefers using fetch and axios to fire AJAX requests because:




          • fetch is a Promise-based API for fetching resources, it is built in every major web browser out there.


          • axios is a small library that does only one thing and does it well, as opposed to jQuery which tries to be the library to do everything web-related.

          Two last things:



          • Fix indentation. Or use Prettier and forget about it completely.

          • Remove all of the comments in your code.





          share|improve this answer

















          • 1




            Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
            – Alex Jolig
            Jun 11 at 4:47






          • 1




            You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
            – continued-
            Jun 11 at 13:03










          • I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
            – Alex Jolig
            Jun 13 at 7:49










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196216%2freact-pagination-without-jsx%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          I recommend you to use JSX and build tools because they are totally awesome!



          People use npm and bundlers (like Webpack) because loading each library separately makes the page significantly slower. Let me show you an extreme example. Feel free to take a peek at the source code. And that's not to mention that it's simpler to update my libraries using npm.



          JSX can help you effortlessly construct the element tree in React. You're missing out a lot if you avoid JSX!



          You can create an alias for React.createElement to make your code more readable.



          const h = React.createElement;



          //NOTE: return elements as array is added in react 16+




          You're right. Did you know that React 16 also has a cooler feature?



          If you used Fragments, you wouldn't need to add keys to the two divs you rendered in the render() method.



          Here's the improved code:



          return h(React.Fragment, null,
          h("div", className: "row" ,
          h("ul", null, renderPosts)
          ),
          h("div", className: "row" ,
          h("ul", className: "pagination" ),
          prevPage,
          renderPageNumbers,
          nextPage
          )
          )


          You don't need to pass null as the second parameter here:



          ReactDOM.render(
          React.createElement(ImageGallery, null),
          document.getElementById("root")
          );




          ReactDOM.render(h(ImageGallery), document.getElementById("root"))


          You don't need to use jQuery(document).ready when you've already put your <script> tags under the container element.



          The React community prefers using fetch and axios to fire AJAX requests because:




          • fetch is a Promise-based API for fetching resources, it is built in every major web browser out there.


          • axios is a small library that does only one thing and does it well, as opposed to jQuery which tries to be the library to do everything web-related.

          Two last things:



          • Fix indentation. Or use Prettier and forget about it completely.

          • Remove all of the comments in your code.





          share|improve this answer

















          • 1




            Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
            – Alex Jolig
            Jun 11 at 4:47






          • 1




            You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
            – continued-
            Jun 11 at 13:03










          • I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
            – Alex Jolig
            Jun 13 at 7:49














          up vote
          2
          down vote



          accepted










          I recommend you to use JSX and build tools because they are totally awesome!



          People use npm and bundlers (like Webpack) because loading each library separately makes the page significantly slower. Let me show you an extreme example. Feel free to take a peek at the source code. And that's not to mention that it's simpler to update my libraries using npm.



          JSX can help you effortlessly construct the element tree in React. You're missing out a lot if you avoid JSX!



          You can create an alias for React.createElement to make your code more readable.



          const h = React.createElement;



          //NOTE: return elements as array is added in react 16+




          You're right. Did you know that React 16 also has a cooler feature?



          If you used Fragments, you wouldn't need to add keys to the two divs you rendered in the render() method.



          Here's the improved code:



          return h(React.Fragment, null,
          h("div", className: "row" ,
          h("ul", null, renderPosts)
          ),
          h("div", className: "row" ,
          h("ul", className: "pagination" ),
          prevPage,
          renderPageNumbers,
          nextPage
          )
          )


          You don't need to pass null as the second parameter here:



          ReactDOM.render(
          React.createElement(ImageGallery, null),
          document.getElementById("root")
          );




          ReactDOM.render(h(ImageGallery), document.getElementById("root"))


          You don't need to use jQuery(document).ready when you've already put your <script> tags under the container element.



          The React community prefers using fetch and axios to fire AJAX requests because:




          • fetch is a Promise-based API for fetching resources, it is built in every major web browser out there.


          • axios is a small library that does only one thing and does it well, as opposed to jQuery which tries to be the library to do everything web-related.

          Two last things:



          • Fix indentation. Or use Prettier and forget about it completely.

          • Remove all of the comments in your code.





          share|improve this answer

















          • 1




            Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
            – Alex Jolig
            Jun 11 at 4:47






          • 1




            You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
            – continued-
            Jun 11 at 13:03










          • I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
            – Alex Jolig
            Jun 13 at 7:49












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          I recommend you to use JSX and build tools because they are totally awesome!



          People use npm and bundlers (like Webpack) because loading each library separately makes the page significantly slower. Let me show you an extreme example. Feel free to take a peek at the source code. And that's not to mention that it's simpler to update my libraries using npm.



          JSX can help you effortlessly construct the element tree in React. You're missing out a lot if you avoid JSX!



          You can create an alias for React.createElement to make your code more readable.



          const h = React.createElement;



          //NOTE: return elements as array is added in react 16+




          You're right. Did you know that React 16 also has a cooler feature?



          If you used Fragments, you wouldn't need to add keys to the two divs you rendered in the render() method.



          Here's the improved code:



          return h(React.Fragment, null,
          h("div", className: "row" ,
          h("ul", null, renderPosts)
          ),
          h("div", className: "row" ,
          h("ul", className: "pagination" ),
          prevPage,
          renderPageNumbers,
          nextPage
          )
          )


          You don't need to pass null as the second parameter here:



          ReactDOM.render(
          React.createElement(ImageGallery, null),
          document.getElementById("root")
          );




          ReactDOM.render(h(ImageGallery), document.getElementById("root"))


          You don't need to use jQuery(document).ready when you've already put your <script> tags under the container element.



          The React community prefers using fetch and axios to fire AJAX requests because:




          • fetch is a Promise-based API for fetching resources, it is built in every major web browser out there.


          • axios is a small library that does only one thing and does it well, as opposed to jQuery which tries to be the library to do everything web-related.

          Two last things:



          • Fix indentation. Or use Prettier and forget about it completely.

          • Remove all of the comments in your code.





          share|improve this answer













          I recommend you to use JSX and build tools because they are totally awesome!



          People use npm and bundlers (like Webpack) because loading each library separately makes the page significantly slower. Let me show you an extreme example. Feel free to take a peek at the source code. And that's not to mention that it's simpler to update my libraries using npm.



          JSX can help you effortlessly construct the element tree in React. You're missing out a lot if you avoid JSX!



          You can create an alias for React.createElement to make your code more readable.



          const h = React.createElement;



          //NOTE: return elements as array is added in react 16+




          You're right. Did you know that React 16 also has a cooler feature?



          If you used Fragments, you wouldn't need to add keys to the two divs you rendered in the render() method.



          Here's the improved code:



          return h(React.Fragment, null,
          h("div", className: "row" ,
          h("ul", null, renderPosts)
          ),
          h("div", className: "row" ,
          h("ul", className: "pagination" ),
          prevPage,
          renderPageNumbers,
          nextPage
          )
          )


          You don't need to pass null as the second parameter here:



          ReactDOM.render(
          React.createElement(ImageGallery, null),
          document.getElementById("root")
          );




          ReactDOM.render(h(ImageGallery), document.getElementById("root"))


          You don't need to use jQuery(document).ready when you've already put your <script> tags under the container element.



          The React community prefers using fetch and axios to fire AJAX requests because:




          • fetch is a Promise-based API for fetching resources, it is built in every major web browser out there.


          • axios is a small library that does only one thing and does it well, as opposed to jQuery which tries to be the library to do everything web-related.

          Two last things:



          • Fix indentation. Or use Prettier and forget about it completely.

          • Remove all of the comments in your code.






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 10 at 9:23









          continued-

          563




          563







          • 1




            Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
            – Alex Jolig
            Jun 11 at 4:47






          • 1




            You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
            – continued-
            Jun 11 at 13:03










          • I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
            – Alex Jolig
            Jun 13 at 7:49












          • 1




            Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
            – Alex Jolig
            Jun 11 at 4:47






          • 1




            You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
            – continued-
            Jun 11 at 13:03










          • I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
            – Alex Jolig
            Jun 13 at 7:49







          1




          1




          Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
          – Alex Jolig
          Jun 11 at 4:47




          Thanks for the heads ups. I'll try changing AJAX request to one of your offers. But the reason I wanted to do it without JSX is that I don't want to have to work with npm and stuffs when I'm going to use just a small component like this in a single page. I couldn't find any ways to use JSX without using npm.
          – Alex Jolig
          Jun 11 at 4:47




          1




          1




          You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
          – continued-
          Jun 11 at 13:03




          You are right, if you are only using React for a small component on a large non-React web app then using JSX and build tools only adds overhead. And no, I am not giving you any heads up! ;P
          – continued-
          Jun 11 at 13:03












          I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
          – Alex Jolig
          Jun 13 at 7:49




          I've published the professional version of my pagination and used your ideas. Thank you. Professional pagination using react
          – Alex Jolig
          Jun 13 at 7:49












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196216%2freact-pagination-without-jsx%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods