dancormier / react-native-swipeout

iOS-style swipeout buttons behind component
MIT License
2.62k stars 646 forks source link

unable to use onOpen #281

Closed kingtross88 closed 6 years ago

kingtross88 commented 6 years ago

I'm currently trying to use onOpen within my render function. I have a if statement to render 4 different view depending on the data in the array. My 'onOpen' prop keeps stating this.setState isn't a function. Can someone please assist me with what I'm doing wrong? See code below.

if (collection.item.type == 'car') { return ( <Swipeout onOpen={(index) => { this.setState({ index, }); }} style={{backgroundColor: 'rgba(0,0,0,0)',}} right={swipeoutBtns}> <LinearGradient style={{justifyContent: 'center', borderRadius: 30, width: 250, height: 180, alignSelf: 'center'}} colors={['#ff00ff', '#0066ff']}> <View style={{alignSelf: 'center', justifyContent: 'center', width: 60, height: 60, borderRadius: 60/2, backgroundColor: 'white'}}> <Text style={{alignSelf: 'center'}}> {car} <Text style={{fontWeight: 'bold', marginTop: 5, color: 'white', fontSize: 16, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}> {collection.item.make} <Text style={{fontWeight: 'bold', marginTop: 5, color: 'white', fontSize: 16, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}> {collection.item.model} <Text style={{fontWeight: 'bold', marginTop: 5, color: 'white', fontSize: 16, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}> {collection.item.color} <Text style={{fontWeight: 'bold', marginTop: 5, color: 'white', fontSize: 16, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}> {collection.item.year} <Text style={{fontWeight: 'bold', color: 'white', fontSize: 16, alignSelf: 'center', backgroundColor: 'rgba(0,0,0,0)',}}> {collection.item.licensePlate} ); }

hugpeter commented 6 years ago

maybe your function is not properly bound to the component. try writing it like this

onOpen={ (index) => this.setState({index}) }

or outside of render, you could write

myfunction = (index) => { this.setState({index}) }

and then onOpen={(index) => myfunction(index) }