简体   繁体   English

React 和 Redux 丢失状态

[英]React and Redux losing State

Hope you all are fine.希望你们都没事。 I am new to react redux world.我是反应 redux 世界的新手。 I am learning and working on a call logging project.我正在学习和从事通话记录项目。 I have a few questions and it would great if someone can guide me whether I am doing it wrong or tell me the alternative.我有几个问题,如果有人能指导我是我做错了还是告诉我替代方案,那就太好了。

I am using JWT to authenticate a user.我正在使用 JWT 对用户进行身份验证。 Once the user details are verified.验证用户详细信息后。 I am dispatching success action and in the reducer, I am setting the state to authenticated true and the response.我正在调度成功操作,并且在减速器中,我将状态设置为经过身份验证的 true 和响应。 I am also storing the token and expiryTime in localStorage我还将令牌和到期时间存储在 localStorage 中

In the root file which is index file.在作为索引文件的根文件中。 I am checking if the token exists in localStorage and if so then dispatching sign in action.我正在检查令牌是否存在于 localStorage 中,如果存在,则调度登录操作。

Everything is working.一切正常。 But I am losing other values like a response from a server.但是我正在丢失其他值,例如来自服务器的响应。 When he logged in for the first time.当他第一次登录时。 How can I tackle this problem ?我该如何解决这个问题?

Secondly, there is a User initial icon on the top right corner.其次,右上角有一个用户初始图标。 I get the initial when a user logs in and it gets stored in auth state.当用户登录时,我得到初始值并且它被存储在 auth 状态。 it works fine but once again if I refresh the page it becomes null and I lose that initial.它工作正常,但如果我刷新页面,它会再次变为空并且我丢失了那个初始值。

so I tried another way and stored the initial in localStorage.所以我尝试了另一种方式并将初始值存储在 localStorage 中。 But the navbar already rendered on the screen and I don't see any initial until I refresh the page.但是导航栏已经呈现在屏幕上,在刷新页面之前我看不到任何初始内容。

I have passed new key in mapStateToProps.我已经在 mapStateToProps 中传递了新键。 ii and stored initial from localStorage in it and it working fine. ii 并将 localStorage 中的初始值存储在其中,并且工作正常。 Is this a valid way of doing it ???这是一种有效的方法吗???

Regards Meet问候见面

            const  SignedInLinks  = (props) => {                                                      
            return (
                <ul  className="right">
                    <li><NavLink  to="/signin"  onClick=        
                    {props.signOut}>Log Out</NavLink></li>
                    <li><NavLink  className="btn btn-floating pink lighten-1"  to="/">
                    {props.ii  ?  props.ii  :  null  }
                    </NavLink></li>
            </ul>
            )}

            const  mapStateToProps  =  state  => {
                return {
                    auth:  state.auth,
                    ii:  window.localStorage.getItem('ui')
                }
            }
            export  default  connect(mapStateToProps, { signOut })(SignedInLinks);

项目图片

Rather than using localStorage in mapStateToProps , intialize your ii state in your reducer corresponding to that state and then pass it to your component via mapStateToProps .而不是在mapStateToProps使用localStorage ,而是在对应于该状态的减速器中初始化您的ii状态,然后通过mapStateToProps将其传递给您的组件。 Something like this.像这样的东西。

const iiReducer = (state = window.localStorage.getItem('ui') || false, action) => {
  /*.
  .
  . Other Logic
  .
  .*/
  return state
}

and then use it normally as you would from a store's state然后像从商店的状态一样正常使用它

const  mapStateToProps  =  state  => {
    return {
        auth: state.auth,
        ii: state.ii
    }
}

Hope this helps !希望这可以帮助 !

I believe I have an idea of what the problem is (I'm kind of a beginner in react and redux aswell, so tell me if I'm speaking nonsense).我相信我知道问题是什么(我也是 react 和 redux 的初学者,所以如果我在说废话,请告诉我)。

You say that you store the token in localstorage (it is recommended not to do that btw, the recommended way is to store it in a cookie), and if a valid token is found in localstorage, you log in. I'm guessing that you store the response from the server (including the icon) in the app's state, or in the redux store?您说您将令牌存储在 localstorage 中(顺便说一句,建议不要这样做,推荐的方法是将其存储在 cookie 中),如果在 localstorage 中找到有效的令牌,则登录。我猜您将来自服务器的响应(包括图标)存储在应用程序的状态中,还是存储在 redux 存储中? If that is the case, this information will be removed when you update the browser (f5),therefore not being loaded anymore.如果是这种情况,当您更新浏览器 (f5) 时,此信息将被删除,因此不再加载。

The solution is to make sure to load the relevant data when the component mounts, so in your componentDidMount method (if you don't have one, make one), and set the state in that method.解决方案是确保在组件挂载时加载相关数据,因此在您的 componentDidMount 方法中(如果您没有,请制作一个),并在该方法中设置状态。

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

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