undefined is not an object (evaluating 'this.refs.modal1')
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?
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?
did you solve this problem?
@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'
},
})
@jackbillstrom you didn't bind your method to the instance.
Add this.openModal1 = this.openModal1.bind(this) in your constructor.
Thanks for the fast reply, closing this then due to my forgotten implementation. Cheers.
@maxs15 Thanks a lot! Solved this problem by add "bind(this)" a few minutes ago