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

undefined is not an object (evaluating 'this.refs.modal1')

Open chstavridis opened this issue 10 years ago • 6 comments

I do not understand why i have the issue mentioned on the title. I am doing everything like the example. Did anyone have any solution?

chstavridis avatar Sep 25 '16 20:09 chstavridis

Did you ever resolve this @chstavridis? Please share a code sample so the problem can be understood. It may be that the component was not yet created and added to refs at the time you are attempting to access it, what part of the lifecycle are you attempting to access it from and how?

gavinlynch avatar Oct 30 '16 05:10 gavinlynch

did you solve this problem?

xzyaoi avatar Nov 14 '16 10:11 xzyaoi

@chstavridis Solved? I'm bumping into this error as well, can't seem to find a solution. Could be wrong with my code? Or if maybe @gavinlynch @stevefermi has solved it?

export class Mainfeed extends Component {

	constructor(props) {
    super(props);
    this.state = {
      dataSource: ds.cloneWithRows(this._videolist())
    }
  }

   _videolist(){
    var dataBlob = []
    for (var ii = 0; ii < videos.length; ii++) {
      dataBlob.push(videos[ii].yt_id)
    }
    return dataBlob
  }

  _renderRow(rowData) {
    return (
    <Card>
      <Image
        styleName="medium-wide"
        source={{uri: `https://img.youtube.com/vi/${rowData}/hqdefault.jpg`}}
      />
      <View styleName="content">
        <Subtitle>{rowData}</Subtitle>
        <Caption>UDH</Caption>
      </View>
    </Card>)
  }

  openModal1(id) {
    this.refs.modal1.open();
  }

  toggleSwipeToClose() {
    this.setState({swipeToClose: !this.state.swipeToClose});
  }

  onClose() {
    console.log('Modal just closed');
  }

  onOpen() {
    console.log('Modal just openned');
  }

  onClosingState(state) {
    console.log('the open/close of the swipeToClose just changed');
  }

  render() {
    return(
    <View>
  	 <ListView
       dataSource={this.state.dataSource}
       renderRow={ this._renderRow } />
      <TouchableHighlight onPress={this.openModal1}><Text>Basic modal</Text></TouchableHighlight>
       <Modal style={[styles.modal, styles.modal1]} ref={"modal1"} swipeToClose={this.state.swipeToClose} onClosed={this.onClose} onOpened={this.onOpen} onClosingState={this.onClosingState}>
          <Text style={styles.text}>Basic modal</Text>
          <TouchableHighlight onPress={this.toggleSwipeToClose} style={styles.btn}><Text>Disable swipeToClose({this.state.swipeToClose ? "true" : "false"})</Text></TouchableHighlight>
        </Modal>
    </View>
		)
  }
}


const styles = StyleSheet.create({
  modal: {
    justifyContent: 'center',
    alignItems: 'center'
  },
})

swe-jack avatar Nov 21 '16 20:11 swe-jack

@jackbillstrom you didn't bind your method to the instance. Add this.openModal1 = this.openModal1.bind(this) in your constructor.

maxs15 avatar Nov 21 '16 20:11 maxs15

Thanks for the fast reply, closing this then due to my forgotten implementation. Cheers.

swe-jack avatar Nov 21 '16 21:11 swe-jack

@maxs15 Thanks a lot! Solved this problem by add "bind(this)" a few minutes ago

openingax avatar Aug 10 '17 08:08 openingax