React how to render page after query change in URL










2















I'm facing a problem :



I access a URL, for example:



http://localhost:900/user



In my page i have an array. When I click on an pagination button for change page in array, my fonction changePage is called, the URL changes for add search query, my page re render because i use setState(page), so far so good. New URL : http://localhost:900/user?page=2 (Attention the pathname has not changed, i just add search query)



If i click on next page again, the url change to http://localhost:900/user?page=3 and my page re render. It's perfect.



Now, i press the back button in the Browser, the location changes back to http://localhost:900/user?page=2 but the page does not re render.



How to re render page if user press the back button in the Browser and not on the last page on application ?



I use :

react : "16.3.2",

react-dom : "16.3.2",

react-redux : "5.0.7",

react-router-dom : "4.2.2",



EDIT:
when i use listen function after create BrowserHistory, and i press previous browser page:



const history = createBrowserHistory();
history.listen((location, action) =>
// location is an object like window.location
console.log(action, location.search);
);


the location.search was good.



My problem is only when i use the arrows of the browser in the same page. When i go in other page and i go back to the page, the url is good to and my array is reload with the good informations










share|improve this question
























  • did you setState in listener?

    – Pravin
    Nov 12 '18 at 13:20











  • when I click on the button next page of my table, i call function fetchDatas. In this function i call an action for fetchData and i use setState. In setState callback, i call other function that use this.props.history.push('/yourNewUrl')

    – A.Vincent
    Nov 12 '18 at 13:31











  • I have created this codesandbox.io/s/z226wn416m and the Main component always rerendered when query string changed. Have a look at that.

    – im_tsm
    Nov 12 '18 at 13:32











  • thanks, i'm going to start modifying my code to try your solution. I will keep you informed.

    – A.Vincent
    Nov 12 '18 at 13:37











  • but your history listener should change the state of your component. browser's back button is not invoking any state change in your component.

    – Pravin
    Nov 12 '18 at 13:40















2















I'm facing a problem :



I access a URL, for example:



http://localhost:900/user



In my page i have an array. When I click on an pagination button for change page in array, my fonction changePage is called, the URL changes for add search query, my page re render because i use setState(page), so far so good. New URL : http://localhost:900/user?page=2 (Attention the pathname has not changed, i just add search query)



If i click on next page again, the url change to http://localhost:900/user?page=3 and my page re render. It's perfect.



Now, i press the back button in the Browser, the location changes back to http://localhost:900/user?page=2 but the page does not re render.



How to re render page if user press the back button in the Browser and not on the last page on application ?



I use :

react : "16.3.2",

react-dom : "16.3.2",

react-redux : "5.0.7",

react-router-dom : "4.2.2",



EDIT:
when i use listen function after create BrowserHistory, and i press previous browser page:



const history = createBrowserHistory();
history.listen((location, action) =>
// location is an object like window.location
console.log(action, location.search);
);


the location.search was good.



My problem is only when i use the arrows of the browser in the same page. When i go in other page and i go back to the page, the url is good to and my array is reload with the good informations










share|improve this question
























  • did you setState in listener?

    – Pravin
    Nov 12 '18 at 13:20











  • when I click on the button next page of my table, i call function fetchDatas. In this function i call an action for fetchData and i use setState. In setState callback, i call other function that use this.props.history.push('/yourNewUrl')

    – A.Vincent
    Nov 12 '18 at 13:31











  • I have created this codesandbox.io/s/z226wn416m and the Main component always rerendered when query string changed. Have a look at that.

    – im_tsm
    Nov 12 '18 at 13:32











  • thanks, i'm going to start modifying my code to try your solution. I will keep you informed.

    – A.Vincent
    Nov 12 '18 at 13:37











  • but your history listener should change the state of your component. browser's back button is not invoking any state change in your component.

    – Pravin
    Nov 12 '18 at 13:40













2












2








2








I'm facing a problem :



I access a URL, for example:



http://localhost:900/user



In my page i have an array. When I click on an pagination button for change page in array, my fonction changePage is called, the URL changes for add search query, my page re render because i use setState(page), so far so good. New URL : http://localhost:900/user?page=2 (Attention the pathname has not changed, i just add search query)



If i click on next page again, the url change to http://localhost:900/user?page=3 and my page re render. It's perfect.



Now, i press the back button in the Browser, the location changes back to http://localhost:900/user?page=2 but the page does not re render.



How to re render page if user press the back button in the Browser and not on the last page on application ?



I use :

react : "16.3.2",

react-dom : "16.3.2",

react-redux : "5.0.7",

react-router-dom : "4.2.2",



EDIT:
when i use listen function after create BrowserHistory, and i press previous browser page:



const history = createBrowserHistory();
history.listen((location, action) =>
// location is an object like window.location
console.log(action, location.search);
);


the location.search was good.



My problem is only when i use the arrows of the browser in the same page. When i go in other page and i go back to the page, the url is good to and my array is reload with the good informations










share|improve this question
















I'm facing a problem :



I access a URL, for example:



http://localhost:900/user



In my page i have an array. When I click on an pagination button for change page in array, my fonction changePage is called, the URL changes for add search query, my page re render because i use setState(page), so far so good. New URL : http://localhost:900/user?page=2 (Attention the pathname has not changed, i just add search query)



If i click on next page again, the url change to http://localhost:900/user?page=3 and my page re render. It's perfect.



Now, i press the back button in the Browser, the location changes back to http://localhost:900/user?page=2 but the page does not re render.



How to re render page if user press the back button in the Browser and not on the last page on application ?



I use :

react : "16.3.2",

react-dom : "16.3.2",

react-redux : "5.0.7",

react-router-dom : "4.2.2",



EDIT:
when i use listen function after create BrowserHistory, and i press previous browser page:



const history = createBrowserHistory();
history.listen((location, action) =>
// location is an object like window.location
console.log(action, location.search);
);


the location.search was good.



My problem is only when i use the arrows of the browser in the same page. When i go in other page and i go back to the page, the url is good to and my array is reload with the good informations







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 14:44







A.Vincent

















asked Nov 12 '18 at 12:51









A.VincentA.Vincent

487




487












  • did you setState in listener?

    – Pravin
    Nov 12 '18 at 13:20











  • when I click on the button next page of my table, i call function fetchDatas. In this function i call an action for fetchData and i use setState. In setState callback, i call other function that use this.props.history.push('/yourNewUrl')

    – A.Vincent
    Nov 12 '18 at 13:31











  • I have created this codesandbox.io/s/z226wn416m and the Main component always rerendered when query string changed. Have a look at that.

    – im_tsm
    Nov 12 '18 at 13:32











  • thanks, i'm going to start modifying my code to try your solution. I will keep you informed.

    – A.Vincent
    Nov 12 '18 at 13:37











  • but your history listener should change the state of your component. browser's back button is not invoking any state change in your component.

    – Pravin
    Nov 12 '18 at 13:40

















  • did you setState in listener?

    – Pravin
    Nov 12 '18 at 13:20











  • when I click on the button next page of my table, i call function fetchDatas. In this function i call an action for fetchData and i use setState. In setState callback, i call other function that use this.props.history.push('/yourNewUrl')

    – A.Vincent
    Nov 12 '18 at 13:31











  • I have created this codesandbox.io/s/z226wn416m and the Main component always rerendered when query string changed. Have a look at that.

    – im_tsm
    Nov 12 '18 at 13:32











  • thanks, i'm going to start modifying my code to try your solution. I will keep you informed.

    – A.Vincent
    Nov 12 '18 at 13:37











  • but your history listener should change the state of your component. browser's back button is not invoking any state change in your component.

    – Pravin
    Nov 12 '18 at 13:40
















did you setState in listener?

– Pravin
Nov 12 '18 at 13:20





did you setState in listener?

– Pravin
Nov 12 '18 at 13:20













when I click on the button next page of my table, i call function fetchDatas. In this function i call an action for fetchData and i use setState. In setState callback, i call other function that use this.props.history.push('/yourNewUrl')

– A.Vincent
Nov 12 '18 at 13:31





when I click on the button next page of my table, i call function fetchDatas. In this function i call an action for fetchData and i use setState. In setState callback, i call other function that use this.props.history.push('/yourNewUrl')

– A.Vincent
Nov 12 '18 at 13:31













I have created this codesandbox.io/s/z226wn416m and the Main component always rerendered when query string changed. Have a look at that.

– im_tsm
Nov 12 '18 at 13:32





I have created this codesandbox.io/s/z226wn416m and the Main component always rerendered when query string changed. Have a look at that.

– im_tsm
Nov 12 '18 at 13:32













thanks, i'm going to start modifying my code to try your solution. I will keep you informed.

– A.Vincent
Nov 12 '18 at 13:37





thanks, i'm going to start modifying my code to try your solution. I will keep you informed.

– A.Vincent
Nov 12 '18 at 13:37













but your history listener should change the state of your component. browser's back button is not invoking any state change in your component.

– Pravin
Nov 12 '18 at 13:40





but your history listener should change the state of your component. browser's back button is not invoking any state change in your component.

– Pravin
Nov 12 '18 at 13:40












3 Answers
3






active

oldest

votes


















1














You can try listening on the 'popstate' event handler :



window.addEventListener("popstate", () => 
// React state change logic
);


From MDN: The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.






share|improve this answer























  • thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

    – A.Vincent
    Nov 13 '18 at 11:05


















0














On the function you are using to route, have you tried: this.props.history.push('/yourNewUrl') ?






share|improve this answer























  • Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

    – A.Vincent
    Nov 12 '18 at 12:58



















0














You need to put appropriate cache-control headers in your response headers -



cache-control: private, max-age=0, no-cache, no-store





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',
    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%2f53262576%2freact-how-to-render-page-after-query-change-in-url%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can try listening on the 'popstate' event handler :



    window.addEventListener("popstate", () => 
    // React state change logic
    );


    From MDN: The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.






    share|improve this answer























    • thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

      – A.Vincent
      Nov 13 '18 at 11:05















    1














    You can try listening on the 'popstate' event handler :



    window.addEventListener("popstate", () => 
    // React state change logic
    );


    From MDN: The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.






    share|improve this answer























    • thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

      – A.Vincent
      Nov 13 '18 at 11:05













    1












    1








    1







    You can try listening on the 'popstate' event handler :



    window.addEventListener("popstate", () => 
    // React state change logic
    );


    From MDN: The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.






    share|improve this answer













    You can try listening on the 'popstate' event handler :



    window.addEventListener("popstate", () => 
    // React state change logic
    );


    From MDN: The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 14:54









    DmitriyDmitriy

    490112




    490112












    • thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

      – A.Vincent
      Nov 13 '18 at 11:05

















    • thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

      – A.Vincent
      Nov 13 '18 at 11:05
















    thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

    – A.Vincent
    Nov 13 '18 at 11:05





    thx you so much. i have call this function on componentDidMount() and removeEventListener in componentWillUnmount()

    – A.Vincent
    Nov 13 '18 at 11:05













    0














    On the function you are using to route, have you tried: this.props.history.push('/yourNewUrl') ?






    share|improve this answer























    • Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

      – A.Vincent
      Nov 12 '18 at 12:58
















    0














    On the function you are using to route, have you tried: this.props.history.push('/yourNewUrl') ?






    share|improve this answer























    • Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

      – A.Vincent
      Nov 12 '18 at 12:58














    0












    0








    0







    On the function you are using to route, have you tried: this.props.history.push('/yourNewUrl') ?






    share|improve this answer













    On the function you are using to route, have you tried: this.props.history.push('/yourNewUrl') ?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 12:55









    kbarkbar

    427




    427












    • Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

      – A.Vincent
      Nov 12 '18 at 12:58


















    • Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

      – A.Vincent
      Nov 12 '18 at 12:58

















    Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

    – A.Vincent
    Nov 12 '18 at 12:58






    Yes, in my function i use this.props.history.push('/yourNewUrl') it's for this my url was good when i press previuos button in the browser. But my page does not re render.

    – A.Vincent
    Nov 12 '18 at 12:58












    0














    You need to put appropriate cache-control headers in your response headers -



    cache-control: private, max-age=0, no-cache, no-store





    share|improve this answer



























      0














      You need to put appropriate cache-control headers in your response headers -



      cache-control: private, max-age=0, no-cache, no-store





      share|improve this answer

























        0












        0








        0







        You need to put appropriate cache-control headers in your response headers -



        cache-control: private, max-age=0, no-cache, no-store





        share|improve this answer













        You need to put appropriate cache-control headers in your response headers -



        cache-control: private, max-age=0, no-cache, no-store






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 12:57









        mdeoramdeora

        1,6171815




        1,6171815



























            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%2f53262576%2freact-how-to-render-page-after-query-change-in-url%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20