简体   繁体   English

如何彻底清除 redux 商店? 浏览器后退按钮恢复存储

[英]How to clear a redux store completely? Browser back button restores the store

I have an application that has a cart in redux store.我有一个在 redux 商店中有购物车的应用程序。 When user completes the purchase.当用户完成购买时。 I Reset (redux store) the cart when moves out of the Final page.移出最终页面时,我重置(redux 存储)购物车。 But when user clicks the browser back button.但是当用户单击浏览器后退按钮时。 The redux store is back with the previous data. redux 存储返回之前的数据。 How do I clear the redux store completely?如何彻底清除 redux 商店? There is no Login/Registration Required for Purchase.购买无需登录/注册。 I am using Reactjs with Redux (with redux-thunk).我正在使用 Reactjs 和 Redux(使用 redux-thunk)。

When user hits backbutton - (don't take to previous page ie payment page again)当用户点击后退按钮时 - (不要转到上一页,即再次付款页面)

  componentDidMount () {
    window.onpopstate = this.onBackButtonEvent
  }

onBackButtonEvent = (e) => {
        e.preventDefault()
        console.log('in backbutton')
        this.props.history.push('/insta-repair')
        this.props.resetCart()
        window.onpopstate = () => {}
      }

cart action购物车动作

export const resetCart = () => ({
  type: cartActionTypes.RESET_CART,
  data: []
})

cart reducer推车减速机

case CartTypes.RESET_CART:
  return {
    ...initialState,
    item: action.data
  }

The problem问题

When user hits browser back button.当用户点击浏览器后退按钮时。 Then the Previous redux store data is accessible.然后可以访问以前的 redux 存储数据。 Is there a way to completely Reset the Store?有没有办法完全重置商店?

The code in my question was already clearing the Redux store.我问题中的代码已经清除了 Redux 商店。 The problem here was that it making request to server when there was cart_id present in the url, which would fetch redux store data again.这里的问题是,当 url 中存在 cart_id 时,它会向服务器发出请求,这将再次获取 redux 存储数据。

when the user hits back button on the browser.当用户点击浏览器上的后退按钮时。 It would take him to the cart page.它会将他带到购物车页面。 ie www.mysite.com/cart/id and this url would request the server for data and store it in redux store again.www.mysite.com/cart/id并且此 url 将向服务器请求数据并将其再次存储在 redux 存储中。

I wanted to make sure that redux store was cleared at root reducer level.我想确保 redux 存储在根减速器级别被清除。 So I used code from this answer https://stackoverflow.com/a/35641992/6555647所以我使用了这个答案中的代码https://stackoverflow.com/a/35641992/6555647

const reducers= combineReducers({
  config: config,
  cart: cartReducer,
  shop: shop
})

const rootReducer  = (state, action) => {
  if (action.type === 'RESET_CART') {
    state = undefined; // this will set the state to initial automatically
  }
  return reducers(state, action);
}
export default rootReducer;

You can dispatch the 'RESET_CART action anywhere.您可以在任何地方发送 'RESET_CART 操作。

The back button problem后退键问题

I used to redirect user to Order tracking Page.我曾经将用户重定向到订单跟踪页面。 Instead of history.push() I used history.replace()我使用history.replace() 而不是 history.push( )

this.props.history.replace({
    pathname: `${BASE_URL}/success/${orderNumber}`,
    orderNumber: orderNumber
})

You could just do你可以做

this.props.history.replace(`${BASE_URL}/success/${orderNumber}`)

if you don't want to push orderNumber to state of the newly mounted component.如果您不想将 orderNumber 推送到新安装组件的 state

history.replace('/route') will replace your current route with a new given route history.replace('/route') will replace your current route with a new given route

If user hits back, if he would be on the same page.如果用户回击,他是否会在同一页面上。 If he won't redirected to previous page.如果他不会重定向到上一页。 If hits back button again .如果再按返回键 He will go to home page.他将 go 到主页。 I have added code for that in the OrderNumber Component.我在 OrderNumber 组件中为此添加了代码。

  componentDidMount () {
    window.onpopstate = this.onBackButtonEvent
  }
  onBackButtonEvent = (e) => {
    e.preventDefault()
    console.log('in backbutton')
    this.props.history.push('/insta-repair') //homepage route.
    this.props.resetCart()
    window.onpopstate = () => {} // reset the onpopstate
  }

Remember to reset the window.onpopstate or else every back button event will take to same page on every component in your app.请记住重置 window.onpopstate否则每个后退按钮事件将在您的应用程序中的每个组件上进入同一页面。

Read more about history object here - https://reacttraining.com/react-router/web/api/history在此处阅读有关历史 object的更多信息 - https://reacttraining.com/react-router/web/api/history

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM