1
votes

I need to use a switch toggle in my App (library git: dooboolab/react-native-switch-toggle). I'm using typescript in react-native. I get an error when I set the function for the onPress gestureResponderEvent. It says: type void is not assignable to type boolean.

import React, {Component} from 'react';
import SwitchToggle form 'react-native-switch-toggle';
import {View} from 'react-native';

export default class Login extends Component{

  state = {
    switchOn : false
  }

  render(){
    return (
      <View>
        <SwitchToggle 
            switchOn={this.state.switchOn}
            onPress = {this.onPress1}  //HERE THE ERROR
        />
      </View>
    )
  }

  onPress1 = () =>{
     this.setState({switchOn:!this.state.switchOn});
  }
}

How can I fix it?

2
on the onPress1 function can you add a return false; statement and try it - Rinor Dreshaj
It doesn't work yet - palnic

2 Answers

0
votes

onPress is unable to react the function. Call the function from onPress like this

onPress = {() =>this.onPress1() }

0
votes

You need to type your onPress1 method with the same signature that onPress prop has. SwitchToggle's onPress has the following signature:

onPress: (event: GestureResponderEvent) => {};

So in order for you to pass this.onPress1 to onPress prop, you'd need to write your onPress1 in this manner:

onPress1 = (event: GestureResponderEvent) => {
   // do stuff
};

Then in your JSX:

<SwitchToggle onPress={this.onPress1} />