How does one access state on a nested React component wrapped by an HOC?

后端 未结 2 1740
北荒
北荒 2020-12-14 18:47

I\'m using Enzyme, and we can actually use the example component given in the docs as a foundation for my question.

Let\'s assume this com

2条回答
  •  隐瞒了意图╮
    2020-12-14 19:07

    Thought it might be useful for you guys, as I stumbled upon this and have a fix.

    In my case I have a component which is connected to redux.

    class ComponentName extends Component {
    ...
    }
    export default connect(
      mapStateToProps,
      {
    ...
     }
    )(ComponentName );
    

    connect() is obviously a HOC component. So how do we access the "ComponentName" here?

    Very simple:

    component
        .find(ComponentName)
        .children()
        .first()
        .props() // returns ComponentName's props 
    

提交回复
热议问题