react-native-mock icon indicating copy to clipboard operation
react-native-mock copied to clipboard

Ref don't work with mount

Open jnak opened this issue 8 years ago • 6 comments

I'm not sure if it's expected or not. I created a repro here: https://github.com/jnak/enzyme-rn-ref-repro

Very cool project btw!

jnak avatar Feb 26 '16 20:02 jnak

I expect mount should not work as actually mouniting in that case is rendering in native thread.

vitalets avatar Feb 29 '16 09:02 vitalets

@jnak can you provide more details on this so we can try to replicate and fix it

RealOrangeOne avatar May 24 '16 17:05 RealOrangeOne

Same issue, ref is undefined in wrapper instance's componentDidMount. It works when actually running on ios simulator.

COMPONENT

...
componentDidMount() {
  console.log(this.refs); // {}
  console.log(this.nav); // undefined
  this.nav.jumpTo(this.currentRoute); // cannot call method jumpTo on undefined
}
render() {
  return (
    <View style={{ flex: 1 }}>
      <Navigator
        ref={e => this.nav = e}
        renderScene={this.renderScene}
        initialRouteStack={routes}
        sceneStyle={{ flex: 1 }}
        configureScene={() => Object.assign(Navigator.SceneConfigs.HorizontalSwipeJump, { gestures: {} })}
      />
    </View>
  );
}
...

TEST

const view = mount(<MyComponent {...defaultProps} />); // in componentDidMount refs = {}
console.log(view.instance().nav); // undefined

chrismcleod avatar Dec 31 '16 03:12 chrismcleod

i believe passing a string to the refs prop was phased out recently in ReactJS, so react-native has probably followed. I think the problem is that a components ref handler is never called once it's mounted, so 'this.nav' is never set

RealOrangeOne avatar Dec 31 '16 15:12 RealOrangeOne

using old style is a different but similar problem. If i use old-style string ref, this.refs is still {} in componentDidMount, but view.ref("myRef") will return the Navigator wrapper.

chrismcleod avatar Dec 31 '16 19:12 chrismcleod

It looks like maybe it has something to do with refs that are not the root element. If I move the Navigator to the root element, and use the new style, this.nav is set in componentDidMount. Am I doing something wrong in my test or is this an issue with enzyme?

chrismcleod avatar Dec 31 '16 23:12 chrismcleod