Skip to content

[v.7.0.0] Component one update behind #1219

Closed
@luskin

Description

@luskin

What is the current behavior?

After upgrading to v7.0.0-beta.0 our child components are one update behind the state unless we connect the parent component as well.

Here is a simple test component:

// @flow
import React from 'react'
import { connect } from 'react-redux'

type Props = {
  user: Object,
  organization: Object
}

/**
 *
 * @param {user} Object user from redux state
 * @param {organization} Object organization from redux state
 */
export const TestComponent = ({ user, organization }: Props) => {
  console.log('First Name [Component]: ', user.firstName)
  return <div>{user.firstName}</div>
}

export const MSP = ({ user, organization }: Props) => {
  console.log('First Name [MSP]: ', user.firstName)
  return { user, organization }
}
export default connect(MSP)(TestComponent)

Here is the output using react-redux 6.0.1:

[DEBUG]: Initialize user module
First Name [MSP]:  Greg
First Name [Component]:  Greg
--- HERE IS WHERE THE STATE WAS UPDATED ---
First Name [MSP]:  Gregg
First Name [Component]:  Gregg

here is the output using react-redux 7.0.0-beta.0

[DEBUG]: Initialize user module
First Name [MSP]:  Greg
First Name [Component]:  Greg
--- HERE I UPDATED STATE FROM Greg to Gregg ---
First Name [MSP]:  Gregg
--- NOTICE THAT COMPONENT WAS NOT UPDATED ---
--- I THEN PROCEED TO UPDATE THE STATE TO Greggg ---
First Name [MSP]:  Greggg
First Name [Component]:  Gregg

As you can see mapStateToProps requires the state to be modified a second time before updating the component to the previous state. It seems as though possibly the comparison check is not firing off?

Things I have tried:

  • Spreading the objects into a new object (new reference, comparison should be false and thus a rerender), this did not work.
  • Updating the functional component to a PureComponent and then Component, this did not work.
  • Removed all hooks from parent component, this did not work
  • Connecting parent component and mapStateToProps-ing the user object, THIS DID WORK

What is the expected behavior?
Redux should do an equality check and notice that the state is different and update the component props accordingly.

React 16.8.5
React-Redux: 7.0.0-beta.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions