Why do I have to refresh page manually after delete?React-router urls don't work when refreshing or writing manuallyFlask and React routingReactJS - Route update link but not refresh pageThe prop is lost after the page is refreshed in reactProps only render after I refresh the pageRedirection when typing in links manually, other issues with react-router propsReactjs prop won't populate field when page is refreshedReact redirect not displaying correct componentReact Router is making my authentication-conditional view redirect even when authenticated?How can I get <Redirect … /> to refresh the page after redirect in reactjs

Share calendar details request from manager's manager

How to forge a multi-part weapon?

How can I get an unreasonable manager to approve time off?

Why is one of Madera Municipal's runways labelled with only "R" on both sides?

How Often Do Health Insurance Providers Drop Coverage?

SOQL Not Recognizing Field?

Are there downsides to using std::string as a buffer?

How is water heavier than petrol, even though its molecular weight is less than petrol?

Project Euler #7 10001st prime in C++

Why did the Herschel Space Telescope need helium coolant?

Does Disney no longer produce hand-drawn cartoon films?

Find the limit of a multiplying term function when n tends to infinity.

System.StringException: Unexpected end of expression

Fixing obscure 8080 emulator bug?

SQL counting distinct over partition

Why was the Sega Genesis marketed as a 16-bit console?

Medieval flying castle propulsion

Does an ice chest packed full of frozen food need ice?

bash script: "*.jpg" expansion not working as expected inside $(...), for picking a random file

Second (easy access) account in case my bank screws up

Should I give professor gift at the beginning of my PhD?

Déjà vu, again?

Is counterpoint still used today?

Winning Strategy for the Magician and his Apprentice



Why do I have to refresh page manually after delete?


React-router urls don't work when refreshing or writing manuallyFlask and React routingReactJS - Route update link but not refresh pageThe prop is lost after the page is refreshed in reactProps only render after I refresh the pageRedirection when typing in links manually, other issues with react-router propsReactjs prop won't populate field when page is refreshedReact redirect not displaying correct componentReact Router is making my authentication-conditional view redirect even when authenticated?How can I get <Redirect … /> to refresh the page after redirect in reactjs






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















When I delete a circuit (via link on details page) and redirect back to the home page, the object I deleted is still there until I manually refresh the page.



I have tried passing in props through the router:



<Route exact path='/' render=(props) => <CircuitList ...props /> />


I have tried moving the delete function from the detail view container to the list view container, but cannot figure out how to pass the function down through the components.



The delete action



 handleDeleteCircuit = (event) => 
const circuitID = this.props.match.params.id;

axios.delete(`link-to-api`, this.state);
this.props.history.push('/');
this.forceUpdate();



Every time I delete a circuit, it deletes from the api, but when it redirects to the home page, it's still there until I manually refresh the page.










share|improve this question

















  • 1





    Are you refetching the items on componentDidMount in HomePage?

    – Boy With Silver Wings
    Mar 24 at 13:13











  • No, I am deleting them on a detail-view page and redirecting to the homepage.

    – everyday_potato
    Mar 24 at 13:28











  • You need to show how your fetching and setting the data in the first place.

    – Dehan de Croos
    Mar 24 at 13:37

















0















When I delete a circuit (via link on details page) and redirect back to the home page, the object I deleted is still there until I manually refresh the page.



I have tried passing in props through the router:



<Route exact path='/' render=(props) => <CircuitList ...props /> />


I have tried moving the delete function from the detail view container to the list view container, but cannot figure out how to pass the function down through the components.



The delete action



 handleDeleteCircuit = (event) => 
const circuitID = this.props.match.params.id;

axios.delete(`link-to-api`, this.state);
this.props.history.push('/');
this.forceUpdate();



Every time I delete a circuit, it deletes from the api, but when it redirects to the home page, it's still there until I manually refresh the page.










share|improve this question

















  • 1





    Are you refetching the items on componentDidMount in HomePage?

    – Boy With Silver Wings
    Mar 24 at 13:13











  • No, I am deleting them on a detail-view page and redirecting to the homepage.

    – everyday_potato
    Mar 24 at 13:28











  • You need to show how your fetching and setting the data in the first place.

    – Dehan de Croos
    Mar 24 at 13:37













0












0








0








When I delete a circuit (via link on details page) and redirect back to the home page, the object I deleted is still there until I manually refresh the page.



I have tried passing in props through the router:



<Route exact path='/' render=(props) => <CircuitList ...props /> />


I have tried moving the delete function from the detail view container to the list view container, but cannot figure out how to pass the function down through the components.



The delete action



 handleDeleteCircuit = (event) => 
const circuitID = this.props.match.params.id;

axios.delete(`link-to-api`, this.state);
this.props.history.push('/');
this.forceUpdate();



Every time I delete a circuit, it deletes from the api, but when it redirects to the home page, it's still there until I manually refresh the page.










share|improve this question














When I delete a circuit (via link on details page) and redirect back to the home page, the object I deleted is still there until I manually refresh the page.



I have tried passing in props through the router:



<Route exact path='/' render=(props) => <CircuitList ...props /> />


I have tried moving the delete function from the detail view container to the list view container, but cannot figure out how to pass the function down through the components.



The delete action



 handleDeleteCircuit = (event) => 
const circuitID = this.props.match.params.id;

axios.delete(`link-to-api`, this.state);
this.props.history.push('/');
this.forceUpdate();



Every time I delete a circuit, it deletes from the api, but when it redirects to the home page, it's still there until I manually refresh the page.







reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 13:05









everyday_potatoeveryday_potato

74




74







  • 1





    Are you refetching the items on componentDidMount in HomePage?

    – Boy With Silver Wings
    Mar 24 at 13:13











  • No, I am deleting them on a detail-view page and redirecting to the homepage.

    – everyday_potato
    Mar 24 at 13:28











  • You need to show how your fetching and setting the data in the first place.

    – Dehan de Croos
    Mar 24 at 13:37












  • 1





    Are you refetching the items on componentDidMount in HomePage?

    – Boy With Silver Wings
    Mar 24 at 13:13











  • No, I am deleting them on a detail-view page and redirecting to the homepage.

    – everyday_potato
    Mar 24 at 13:28











  • You need to show how your fetching and setting the data in the first place.

    – Dehan de Croos
    Mar 24 at 13:37







1




1





Are you refetching the items on componentDidMount in HomePage?

– Boy With Silver Wings
Mar 24 at 13:13





Are you refetching the items on componentDidMount in HomePage?

– Boy With Silver Wings
Mar 24 at 13:13













No, I am deleting them on a detail-view page and redirecting to the homepage.

– everyday_potato
Mar 24 at 13:28





No, I am deleting them on a detail-view page and redirecting to the homepage.

– everyday_potato
Mar 24 at 13:28













You need to show how your fetching and setting the data in the first place.

– Dehan de Croos
Mar 24 at 13:37





You need to show how your fetching and setting the data in the first place.

– Dehan de Croos
Mar 24 at 13:37












4 Answers
4






active

oldest

votes


















0














What you see in render is what you have in your state.



TL;DR
You need to update your state.




So you have two ways.



  1. Optimistic update, fire delete api to server + setState to delete data in your state. And after you receive response from delete api, act accordingly.


  2. After fire delete api, re-fetch the whole data from server again and update state.


I would suggest option 1 because it feels quicker for user.




Some extra concept here is.



  1. If you want to share state (data) with your "CircuitList" and "CircuitDetail" page. You should store circuits in some parent, like App (or Redux state) and then pass them down to each component.


  2. Again, the optimistic update pattern. Fire API + setState immediately... revert back if things go wrong.


Let me know if you feel like any more help :)




Below is what you can do.



// assuming you have your data in App, state.circuits
class App extends React.Component
state =
circuits: [
id: 1, title: 'Circuit 1' ,
id: 2, title: 'Circuit 2' ,
id: 3, title: 'Circuit 3' ,
],


afterDeleteCircuit = (circuitID) =>

// state, before delete anything
const currentCircuits = this.state.circuits;

// Remove deleted item from state.
this.setState(
circuits: currentCircuits.filter(circuit => circuit.id !== circuitID),
);

// Fire delete API
axios
.delete(`link-to-api`, this.state)
.then(response =>
if (response.status === 'error')
// Oops, something went wrong. Let's get that deleted Id back.
this.setState(
circuits: currentCircuits,
);

// Show Error message here.
else

// Delete successfully, do nothing.
// Because we already remove the deleted id from state.

// Show success message here.



);


render()
return (
<Router>
<Route exact path='/' render=(props) => <CircuitList ...props circuits=this.state.circuits /> />
<Route path='/circuits/:id' render=(props) => <CircuitDetail ...props onDelete=this.afterDeleteCircuit /> />

</Router>
);




class CircuitList extends React.Component

render ()
const circuits = this.props;
return (
<div>
circuits.map(circuit => (
<li>circuit.title <Link to=`/circuit/$circuit.id`>Edit</Link></li>
)
</div>
);




class CircuitDetail extends React.Component

handleDelete = (e) =>
const match, history = this.props;
const id = match.params.id;
this.props.onDelete(id);

history.push('/');


render ()
return (
<div>
...
// Delete button
<button onClick=this.handleDelete>Delete</button>
</div>
);








share|improve this answer

























  • Would anything change if my data was in CircuitList vs. App?

    – everyday_potato
    Mar 24 at 14:05











  • Is CircuitDetail in App or CircuitList?

    – Doppio
    Mar 24 at 14:15











  • Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

    – Doppio
    Mar 24 at 14:25












  • Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

    – everyday_potato
    Mar 24 at 17:11











  • Yes you do, I'll update the answer for you.

    – Doppio
    Mar 24 at 17:16


















1














You need to remove elements from the state (if this is in react state then remove from react state. If it is in redux then remove element from the redux state)



Or you can fetch all the items again from backend while your index route is mounting again (in componentDidMount) but as network calls are heavier than state manipulation. I'd go for optimistic approach of deleting the items from the state (whether it's react or redux or any other state management library you're using)



 handleDeleteCircuit = async (event) => 
const circuitID = this.props.match.params.id;
await axios.delete(`link-to-api`, this.state);
const circuits = [...this.state.circuits];
const index = array.indexOf(circuitID)
//or remove element from the redux state
if (index !== -1)
circuits.splice(index, 1);
this.setState(circuits);

this.props.history.push('/');
this.forceUpdate();






share|improve this answer























  • Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

    – everyday_potato
    Mar 24 at 13:35











  • Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

    – Ajay Gaur
    Mar 24 at 13:36











  • Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

    – everyday_potato
    Mar 24 at 13:46











  • You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

    – Ajay Gaur
    Mar 24 at 13:58


















0














You don't have any code that removes the circuit from the circuit list.



Something like this :



let this.state.circuits = this.state.circuits.filter((circuit) => 
return circuit.id == circuitId
)


I'm not familiar with React so maybe this requires some changes.






share|improve this answer






























    0














    If you are using a redux Store to manage the items in the CircuitList, one possible cause is that even though the Entry is deleted via api call, the data still persists in the redux store, which will need to be refreshed aswell once the entry is deleted.






    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%2f55324088%2fwhy-do-i-have-to-refresh-page-manually-after-delete%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      What you see in render is what you have in your state.



      TL;DR
      You need to update your state.




      So you have two ways.



      1. Optimistic update, fire delete api to server + setState to delete data in your state. And after you receive response from delete api, act accordingly.


      2. After fire delete api, re-fetch the whole data from server again and update state.


      I would suggest option 1 because it feels quicker for user.




      Some extra concept here is.



      1. If you want to share state (data) with your "CircuitList" and "CircuitDetail" page. You should store circuits in some parent, like App (or Redux state) and then pass them down to each component.


      2. Again, the optimistic update pattern. Fire API + setState immediately... revert back if things go wrong.


      Let me know if you feel like any more help :)




      Below is what you can do.



      // assuming you have your data in App, state.circuits
      class App extends React.Component
      state =
      circuits: [
      id: 1, title: 'Circuit 1' ,
      id: 2, title: 'Circuit 2' ,
      id: 3, title: 'Circuit 3' ,
      ],


      afterDeleteCircuit = (circuitID) =>

      // state, before delete anything
      const currentCircuits = this.state.circuits;

      // Remove deleted item from state.
      this.setState(
      circuits: currentCircuits.filter(circuit => circuit.id !== circuitID),
      );

      // Fire delete API
      axios
      .delete(`link-to-api`, this.state)
      .then(response =>
      if (response.status === 'error')
      // Oops, something went wrong. Let's get that deleted Id back.
      this.setState(
      circuits: currentCircuits,
      );

      // Show Error message here.
      else

      // Delete successfully, do nothing.
      // Because we already remove the deleted id from state.

      // Show success message here.



      );


      render()
      return (
      <Router>
      <Route exact path='/' render=(props) => <CircuitList ...props circuits=this.state.circuits /> />
      <Route path='/circuits/:id' render=(props) => <CircuitDetail ...props onDelete=this.afterDeleteCircuit /> />

      </Router>
      );




      class CircuitList extends React.Component

      render ()
      const circuits = this.props;
      return (
      <div>
      circuits.map(circuit => (
      <li>circuit.title <Link to=`/circuit/$circuit.id`>Edit</Link></li>
      )
      </div>
      );




      class CircuitDetail extends React.Component

      handleDelete = (e) =>
      const match, history = this.props;
      const id = match.params.id;
      this.props.onDelete(id);

      history.push('/');


      render ()
      return (
      <div>
      ...
      // Delete button
      <button onClick=this.handleDelete>Delete</button>
      </div>
      );








      share|improve this answer

























      • Would anything change if my data was in CircuitList vs. App?

        – everyday_potato
        Mar 24 at 14:05











      • Is CircuitDetail in App or CircuitList?

        – Doppio
        Mar 24 at 14:15











      • Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

        – Doppio
        Mar 24 at 14:25












      • Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

        – everyday_potato
        Mar 24 at 17:11











      • Yes you do, I'll update the answer for you.

        – Doppio
        Mar 24 at 17:16















      0














      What you see in render is what you have in your state.



      TL;DR
      You need to update your state.




      So you have two ways.



      1. Optimistic update, fire delete api to server + setState to delete data in your state. And after you receive response from delete api, act accordingly.


      2. After fire delete api, re-fetch the whole data from server again and update state.


      I would suggest option 1 because it feels quicker for user.




      Some extra concept here is.



      1. If you want to share state (data) with your "CircuitList" and "CircuitDetail" page. You should store circuits in some parent, like App (or Redux state) and then pass them down to each component.


      2. Again, the optimistic update pattern. Fire API + setState immediately... revert back if things go wrong.


      Let me know if you feel like any more help :)




      Below is what you can do.



      // assuming you have your data in App, state.circuits
      class App extends React.Component
      state =
      circuits: [
      id: 1, title: 'Circuit 1' ,
      id: 2, title: 'Circuit 2' ,
      id: 3, title: 'Circuit 3' ,
      ],


      afterDeleteCircuit = (circuitID) =>

      // state, before delete anything
      const currentCircuits = this.state.circuits;

      // Remove deleted item from state.
      this.setState(
      circuits: currentCircuits.filter(circuit => circuit.id !== circuitID),
      );

      // Fire delete API
      axios
      .delete(`link-to-api`, this.state)
      .then(response =>
      if (response.status === 'error')
      // Oops, something went wrong. Let's get that deleted Id back.
      this.setState(
      circuits: currentCircuits,
      );

      // Show Error message here.
      else

      // Delete successfully, do nothing.
      // Because we already remove the deleted id from state.

      // Show success message here.



      );


      render()
      return (
      <Router>
      <Route exact path='/' render=(props) => <CircuitList ...props circuits=this.state.circuits /> />
      <Route path='/circuits/:id' render=(props) => <CircuitDetail ...props onDelete=this.afterDeleteCircuit /> />

      </Router>
      );




      class CircuitList extends React.Component

      render ()
      const circuits = this.props;
      return (
      <div>
      circuits.map(circuit => (
      <li>circuit.title <Link to=`/circuit/$circuit.id`>Edit</Link></li>
      )
      </div>
      );




      class CircuitDetail extends React.Component

      handleDelete = (e) =>
      const match, history = this.props;
      const id = match.params.id;
      this.props.onDelete(id);

      history.push('/');


      render ()
      return (
      <div>
      ...
      // Delete button
      <button onClick=this.handleDelete>Delete</button>
      </div>
      );








      share|improve this answer

























      • Would anything change if my data was in CircuitList vs. App?

        – everyday_potato
        Mar 24 at 14:05











      • Is CircuitDetail in App or CircuitList?

        – Doppio
        Mar 24 at 14:15











      • Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

        – Doppio
        Mar 24 at 14:25












      • Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

        – everyday_potato
        Mar 24 at 17:11











      • Yes you do, I'll update the answer for you.

        – Doppio
        Mar 24 at 17:16













      0












      0








      0







      What you see in render is what you have in your state.



      TL;DR
      You need to update your state.




      So you have two ways.



      1. Optimistic update, fire delete api to server + setState to delete data in your state. And after you receive response from delete api, act accordingly.


      2. After fire delete api, re-fetch the whole data from server again and update state.


      I would suggest option 1 because it feels quicker for user.




      Some extra concept here is.



      1. If you want to share state (data) with your "CircuitList" and "CircuitDetail" page. You should store circuits in some parent, like App (or Redux state) and then pass them down to each component.


      2. Again, the optimistic update pattern. Fire API + setState immediately... revert back if things go wrong.


      Let me know if you feel like any more help :)




      Below is what you can do.



      // assuming you have your data in App, state.circuits
      class App extends React.Component
      state =
      circuits: [
      id: 1, title: 'Circuit 1' ,
      id: 2, title: 'Circuit 2' ,
      id: 3, title: 'Circuit 3' ,
      ],


      afterDeleteCircuit = (circuitID) =>

      // state, before delete anything
      const currentCircuits = this.state.circuits;

      // Remove deleted item from state.
      this.setState(
      circuits: currentCircuits.filter(circuit => circuit.id !== circuitID),
      );

      // Fire delete API
      axios
      .delete(`link-to-api`, this.state)
      .then(response =>
      if (response.status === 'error')
      // Oops, something went wrong. Let's get that deleted Id back.
      this.setState(
      circuits: currentCircuits,
      );

      // Show Error message here.
      else

      // Delete successfully, do nothing.
      // Because we already remove the deleted id from state.

      // Show success message here.



      );


      render()
      return (
      <Router>
      <Route exact path='/' render=(props) => <CircuitList ...props circuits=this.state.circuits /> />
      <Route path='/circuits/:id' render=(props) => <CircuitDetail ...props onDelete=this.afterDeleteCircuit /> />

      </Router>
      );




      class CircuitList extends React.Component

      render ()
      const circuits = this.props;
      return (
      <div>
      circuits.map(circuit => (
      <li>circuit.title <Link to=`/circuit/$circuit.id`>Edit</Link></li>
      )
      </div>
      );




      class CircuitDetail extends React.Component

      handleDelete = (e) =>
      const match, history = this.props;
      const id = match.params.id;
      this.props.onDelete(id);

      history.push('/');


      render ()
      return (
      <div>
      ...
      // Delete button
      <button onClick=this.handleDelete>Delete</button>
      </div>
      );








      share|improve this answer















      What you see in render is what you have in your state.



      TL;DR
      You need to update your state.




      So you have two ways.



      1. Optimistic update, fire delete api to server + setState to delete data in your state. And after you receive response from delete api, act accordingly.


      2. After fire delete api, re-fetch the whole data from server again and update state.


      I would suggest option 1 because it feels quicker for user.




      Some extra concept here is.



      1. If you want to share state (data) with your "CircuitList" and "CircuitDetail" page. You should store circuits in some parent, like App (or Redux state) and then pass them down to each component.


      2. Again, the optimistic update pattern. Fire API + setState immediately... revert back if things go wrong.


      Let me know if you feel like any more help :)




      Below is what you can do.



      // assuming you have your data in App, state.circuits
      class App extends React.Component
      state =
      circuits: [
      id: 1, title: 'Circuit 1' ,
      id: 2, title: 'Circuit 2' ,
      id: 3, title: 'Circuit 3' ,
      ],


      afterDeleteCircuit = (circuitID) =>

      // state, before delete anything
      const currentCircuits = this.state.circuits;

      // Remove deleted item from state.
      this.setState(
      circuits: currentCircuits.filter(circuit => circuit.id !== circuitID),
      );

      // Fire delete API
      axios
      .delete(`link-to-api`, this.state)
      .then(response =>
      if (response.status === 'error')
      // Oops, something went wrong. Let's get that deleted Id back.
      this.setState(
      circuits: currentCircuits,
      );

      // Show Error message here.
      else

      // Delete successfully, do nothing.
      // Because we already remove the deleted id from state.

      // Show success message here.



      );


      render()
      return (
      <Router>
      <Route exact path='/' render=(props) => <CircuitList ...props circuits=this.state.circuits /> />
      <Route path='/circuits/:id' render=(props) => <CircuitDetail ...props onDelete=this.afterDeleteCircuit /> />

      </Router>
      );




      class CircuitList extends React.Component

      render ()
      const circuits = this.props;
      return (
      <div>
      circuits.map(circuit => (
      <li>circuit.title <Link to=`/circuit/$circuit.id`>Edit</Link></li>
      )
      </div>
      );




      class CircuitDetail extends React.Component

      handleDelete = (e) =>
      const match, history = this.props;
      const id = match.params.id;
      this.props.onDelete(id);

      history.push('/');


      render ()
      return (
      <div>
      ...
      // Delete button
      <button onClick=this.handleDelete>Delete</button>
      </div>
      );









      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 24 at 17:21

























      answered Mar 24 at 13:44









      DoppioDoppio

      952611




      952611












      • Would anything change if my data was in CircuitList vs. App?

        – everyday_potato
        Mar 24 at 14:05











      • Is CircuitDetail in App or CircuitList?

        – Doppio
        Mar 24 at 14:15











      • Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

        – Doppio
        Mar 24 at 14:25












      • Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

        – everyday_potato
        Mar 24 at 17:11











      • Yes you do, I'll update the answer for you.

        – Doppio
        Mar 24 at 17:16

















      • Would anything change if my data was in CircuitList vs. App?

        – everyday_potato
        Mar 24 at 14:05











      • Is CircuitDetail in App or CircuitList?

        – Doppio
        Mar 24 at 14:15











      • Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

        – Doppio
        Mar 24 at 14:25












      • Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

        – everyday_potato
        Mar 24 at 17:11











      • Yes you do, I'll update the answer for you.

        – Doppio
        Mar 24 at 17:16
















      Would anything change if my data was in CircuitList vs. App?

      – everyday_potato
      Mar 24 at 14:05





      Would anything change if my data was in CircuitList vs. App?

      – everyday_potato
      Mar 24 at 14:05













      Is CircuitDetail in App or CircuitList?

      – Doppio
      Mar 24 at 14:15





      Is CircuitDetail in App or CircuitList?

      – Doppio
      Mar 24 at 14:15













      Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

      – Doppio
      Mar 24 at 14:25






      Anyway, you could use this principle when designing where to keep state/data.... "data always pass down, event always pass up". So it's an anti-pattern to pass data up (CircuitList => App) before passing it down to CircuitDetail. That's why store data at <App /> make sense in react perspective.

      – Doppio
      Mar 24 at 14:25














      Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

      – everyday_potato
      Mar 24 at 17:11





      Thanks for the tip! I reorganized my app and it deletes but it doesn't redirect to the homepage. Do I need to add `this.history.push('/')?

      – everyday_potato
      Mar 24 at 17:11













      Yes you do, I'll update the answer for you.

      – Doppio
      Mar 24 at 17:16





      Yes you do, I'll update the answer for you.

      – Doppio
      Mar 24 at 17:16













      1














      You need to remove elements from the state (if this is in react state then remove from react state. If it is in redux then remove element from the redux state)



      Or you can fetch all the items again from backend while your index route is mounting again (in componentDidMount) but as network calls are heavier than state manipulation. I'd go for optimistic approach of deleting the items from the state (whether it's react or redux or any other state management library you're using)



       handleDeleteCircuit = async (event) => 
      const circuitID = this.props.match.params.id;
      await axios.delete(`link-to-api`, this.state);
      const circuits = [...this.state.circuits];
      const index = array.indexOf(circuitID)
      //or remove element from the redux state
      if (index !== -1)
      circuits.splice(index, 1);
      this.setState(circuits);

      this.props.history.push('/');
      this.forceUpdate();






      share|improve this answer























      • Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

        – everyday_potato
        Mar 24 at 13:35











      • Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

        – Ajay Gaur
        Mar 24 at 13:36











      • Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

        – everyday_potato
        Mar 24 at 13:46











      • You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

        – Ajay Gaur
        Mar 24 at 13:58















      1














      You need to remove elements from the state (if this is in react state then remove from react state. If it is in redux then remove element from the redux state)



      Or you can fetch all the items again from backend while your index route is mounting again (in componentDidMount) but as network calls are heavier than state manipulation. I'd go for optimistic approach of deleting the items from the state (whether it's react or redux or any other state management library you're using)



       handleDeleteCircuit = async (event) => 
      const circuitID = this.props.match.params.id;
      await axios.delete(`link-to-api`, this.state);
      const circuits = [...this.state.circuits];
      const index = array.indexOf(circuitID)
      //or remove element from the redux state
      if (index !== -1)
      circuits.splice(index, 1);
      this.setState(circuits);

      this.props.history.push('/');
      this.forceUpdate();






      share|improve this answer























      • Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

        – everyday_potato
        Mar 24 at 13:35











      • Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

        – Ajay Gaur
        Mar 24 at 13:36











      • Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

        – everyday_potato
        Mar 24 at 13:46











      • You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

        – Ajay Gaur
        Mar 24 at 13:58













      1












      1








      1







      You need to remove elements from the state (if this is in react state then remove from react state. If it is in redux then remove element from the redux state)



      Or you can fetch all the items again from backend while your index route is mounting again (in componentDidMount) but as network calls are heavier than state manipulation. I'd go for optimistic approach of deleting the items from the state (whether it's react or redux or any other state management library you're using)



       handleDeleteCircuit = async (event) => 
      const circuitID = this.props.match.params.id;
      await axios.delete(`link-to-api`, this.state);
      const circuits = [...this.state.circuits];
      const index = array.indexOf(circuitID)
      //or remove element from the redux state
      if (index !== -1)
      circuits.splice(index, 1);
      this.setState(circuits);

      this.props.history.push('/');
      this.forceUpdate();






      share|improve this answer













      You need to remove elements from the state (if this is in react state then remove from react state. If it is in redux then remove element from the redux state)



      Or you can fetch all the items again from backend while your index route is mounting again (in componentDidMount) but as network calls are heavier than state manipulation. I'd go for optimistic approach of deleting the items from the state (whether it's react or redux or any other state management library you're using)



       handleDeleteCircuit = async (event) => 
      const circuitID = this.props.match.params.id;
      await axios.delete(`link-to-api`, this.state);
      const circuits = [...this.state.circuits];
      const index = array.indexOf(circuitID)
      //or remove element from the redux state
      if (index !== -1)
      circuits.splice(index, 1);
      this.setState(circuits);

      this.props.history.push('/');
      this.forceUpdate();







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 24 at 13:19









      Ajay GaurAjay Gaur

      2,28932036




      2,28932036












      • Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

        – everyday_potato
        Mar 24 at 13:35











      • Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

        – Ajay Gaur
        Mar 24 at 13:36











      • Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

        – everyday_potato
        Mar 24 at 13:46











      • You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

        – Ajay Gaur
        Mar 24 at 13:58

















      • Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

        – everyday_potato
        Mar 24 at 13:35











      • Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

        – Ajay Gaur
        Mar 24 at 13:36











      • Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

        – everyday_potato
        Mar 24 at 13:46











      • You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

        – Ajay Gaur
        Mar 24 at 13:58
















      Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

      – everyday_potato
      Mar 24 at 13:35





      Just so I understand, if I use the first example, this is supposed to manipulate the state on the homepage so the consts and names should be the same?

      – everyday_potato
      Mar 24 at 13:35













      Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

      – Ajay Gaur
      Mar 24 at 13:36





      Obviously not, but then you need to send some callback to this component. So that you can tell homepage about what circuitID is been deleted

      – Ajay Gaur
      Mar 24 at 13:36













      Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

      – everyday_potato
      Mar 24 at 13:46





      Is the array in array.indexOf supposed to reference the new array circuits in your code? Also -- this deletes the object but does not redirect only if I remove this.forceUpdate(); but when I include forceUpdate, it doesn't delete.

      – everyday_potato
      Mar 24 at 13:46













      You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

      – Ajay Gaur
      Mar 24 at 13:58





      You don't have to do forceUpdate if you're changing the route. array.indexOf finds the index of circuitId you want to delete in the array

      – Ajay Gaur
      Mar 24 at 13:58











      0














      You don't have any code that removes the circuit from the circuit list.



      Something like this :



      let this.state.circuits = this.state.circuits.filter((circuit) => 
      return circuit.id == circuitId
      )


      I'm not familiar with React so maybe this requires some changes.






      share|improve this answer



























        0














        You don't have any code that removes the circuit from the circuit list.



        Something like this :



        let this.state.circuits = this.state.circuits.filter((circuit) => 
        return circuit.id == circuitId
        )


        I'm not familiar with React so maybe this requires some changes.






        share|improve this answer

























          0












          0








          0







          You don't have any code that removes the circuit from the circuit list.



          Something like this :



          let this.state.circuits = this.state.circuits.filter((circuit) => 
          return circuit.id == circuitId
          )


          I'm not familiar with React so maybe this requires some changes.






          share|improve this answer













          You don't have any code that removes the circuit from the circuit list.



          Something like this :



          let this.state.circuits = this.state.circuits.filter((circuit) => 
          return circuit.id == circuitId
          )


          I'm not familiar with React so maybe this requires some changes.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 13:14









          SimonDepelchinSimonDepelchin

          988914




          988914





















              0














              If you are using a redux Store to manage the items in the CircuitList, one possible cause is that even though the Entry is deleted via api call, the data still persists in the redux store, which will need to be refreshed aswell once the entry is deleted.






              share|improve this answer



























                0














                If you are using a redux Store to manage the items in the CircuitList, one possible cause is that even though the Entry is deleted via api call, the data still persists in the redux store, which will need to be refreshed aswell once the entry is deleted.






                share|improve this answer

























                  0












                  0








                  0







                  If you are using a redux Store to manage the items in the CircuitList, one possible cause is that even though the Entry is deleted via api call, the data still persists in the redux store, which will need to be refreshed aswell once the entry is deleted.






                  share|improve this answer













                  If you are using a redux Store to manage the items in the CircuitList, one possible cause is that even though the Entry is deleted via api call, the data still persists in the redux store, which will need to be refreshed aswell once the entry is deleted.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 24 at 13:19









                  shubhamshubham

                  261




                  261



























                      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%2f55324088%2fwhy-do-i-have-to-refresh-page-manually-after-delete%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