Redux router - how to replay state after refresh?

后端 未结 2 780
感情败类
感情败类 2021-01-31 19:42

I have a multi-step form application, and I\'m struggling with getting my head around how I can save my redux state and replay it after a refresh for example? Going back/forward

2条回答
  •  -上瘾入骨i
    2021-01-31 19:51

    You can't store state on refresh this is normal. Typically you store these inside cookies or web storage.

    • Cookies are synced between browser, server and are set on every request you perform.
    • Localstorage gives you up to 5MB to store data in and never gets send with the request.

    Redux:

    1. Set localstorage properties when filling in form. (be sure to clean it when complete)
    2. Refresh the page with an incomplete form.
    3. When react mounts your Form component, use componentWillMount method to read the localstorage and dispatch the data to a reducer
    4. The reducer updates the redux-state which in turn updates the component with the properties you got from localstorage.

    If you use redux to store form data dispatch the properties of localstorage.

    If not read the localstorage inside the Form component and set the initial values.

    Hope this helps !

提交回复
热议问题