简体   繁体   English

使用redux使用相同值更新状态时组件未更新

[英]Component not updated when state updated with same values with redux

I have a login flow in my react native app. 我的反应原生应用程序中有一个登录流程。 When the user enters its credentials, an action is dispatch that verifies them and then dispatch an other action in order to modify the state by the reducer. 当用户输入其凭据时,将调度一个操作来验证它们,然后调度其他操作以便通过reducer修改状态。

In my component, I register to changes this way: 在我的组件中,我注册以这种方式更改:

function mapStateToProps(state) {
  return { userConnected: state.user.connected }
}

export default connect(mapStateToProps)(LoginScreen)

And I perform action when new props are received with that function: 当我收到带有该功能的新道具时,我会执行操作:

async componentWillReceiveProps(newProps){
  if(newProps.userConnected){
    this.props.navigation.navigate("Home");
  }else{
    this.showWrongCredentials();
  }
}

The first time when the user enters wrong credentials, the props are updated with connected = false, so it shows the wrong credentials message. 用户第一次输入错误的凭据时,使用connected = false更新道具,因此它显示错误的凭据消息。 If the user clicks another time with some wrong credentials, the state received the same values connected = false , so the method componentWillReceiveProps is not called and I cannot show the wrong credentials message again. 如果用户使用一些错误的凭据单击另一次,则状态会收到相同的值connected = false ,因此不会调用方法componentWillReceiveProps,并且我无法再次显示错误的凭据消息。

How can I do that ? 我怎样才能做到这一点 ? Everytime the state is updated, even if the values are the same, I would like the method componentWillReceiveProps to be fired. 每次状态更新时,即使值相同,我也希望触发方法componentWillReceiveProps

Yes, such behaviour is by design. 是的,这种行为是设计的。

From react-redux doc 来自react-redux doc

If performance is a concern, the best way to improve performance is to skip unnecessary re-renders, so that components only re-render when their data has actually changed 如果考虑性能,提高性能的最佳方法是跳过不必要的重新渲染,以便组件仅在数据实际更改时重新呈现

Or if you like source 或者如果你喜欢来源

Note that selectorFactory is responsible for all caching/memoization of inbound and outbound props. 请注意,selectorFactory负责入站和出站道具的所有缓存/记忆。 Do not use connectAdvanced directly without memoizing results between calls to your selector, otherwise the Connect component will re-render on every state or props change . 如果没有在选择器调用之间记住结果,请不要直接使用connectAdvanced,否则Connect组件将在每个状态或props更改时重新呈现

You mentioned that user clicks when entered credentials. 您提到用户在输入凭据时单击。 I suggest to change some property of store as a result of user click. 我建议用户点击后更改商店的某些属性。 So for every click credentials will be verified and message printed. 因此,对于每次点击,都将验证凭据并打印消息。

Introduce additional variable to store which will be used as flag for every user click after credential has been entered. 在存储凭证之后,将附加变量用于存储,该变量将用作每次用户点击的标记。 It can be named credentialEnteredFlag and set to true every time user entered credentials. 它可以命名为credentialEnteredFlag并在每次用户输入凭据时设置为true Then have action to reset credentialEnteredFlag to false from componentWillReceiveProps . 然后执行操作以从componentWillReceivePropscredentialEnteredFlag重置为false

componentWillReceiveProps will look like componentWillReceiveProps看起来像

async componentWillReceiveProps(newProps){
    if(newProps.credentialEnteredFlag){
        this.props.resetCredentialEnteredFlag();

        if(newProps.userConnected){
            this.props.navigation.navigate("Home");
        }else{
            this.showWrongCredentials();
        }
    }
}

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

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