0

Im new to react and are experimenting a bit with react-native-video. Im trying to change a prop in the react-native-video library by clicking on a touchable element. But Im getting the errormessage:

undefined is not an object (evaluating 'this.state.setState')

Im sure this is an easy problem. I basically just want to find out how to initiate, call and change the state of the props when I am touching the defined Touchable area. In this example I want to change the rate from 0.1 to 1.

Here is my code:

type Props = {};
export default class App extends Component<Props> {
  state = {
    rate: 0.1,
  };

  _onPressButton() {
    Alert.alert('You tapped the button!')
    this.state.setState({ rate: 1 });
  }

  render() {

    return (
      <View style={styles.container}>

        <Video
          source={require('./assets/grid.mp4')}
          ref={(ref) => {
            this.player = ref
          }}                                      
          onBuffer={this.onBuffer}                
          onError={this.videoError}               
          style={styles.backgroundVideo}
          rate={this.state.rate}
        />

        <TouchableWithoutFeedback onPress={this._onPressButton}>
          <View style={styles.square1}>
            <Text style={styles.welcome}>My text</Text>
          </View>
        </TouchableWithoutFeedback>
      </View>
    );
  }
}
2
  • sorry. it wasnt meant to. I accepted two answers and that wasnt possible it seems. Thanks! Commented Jun 17, 2019 at 13:16
  • Unfortunately you cannot accept two answers, but you can change your mind at any time. But of course it's up to you, to decide who has the best answer. But never mind Commented Jun 17, 2019 at 13:17

3 Answers 3

1

You are not binding your function.

_onPressButton() {
    Alert.alert('You tapped the button!')
    this.state.setState({ rate: 1 });
  }

should be an arrow function like this

_onPressButton = () => {
    Alert.alert('You tapped the button!')
    this.state.setState({ rate: 1 });
  }

or you need to make a constructor and write this._onPressButton.bind(this) in it.

Sign up to request clarification or add additional context in comments.

Comments

1

As the error mentions:

undefined is not an object (evaluating 'this.state.setState')

this.state has no object called setState

Change:

_onPressButton() {
    Alert.alert('You tapped the button!')
    this.state.setState({ rate: 1 });
  }

To:

_onPressButton() {
    Alert.alert('You tapped the button!')
    this.setState({ rate: 1 });
  }

Additionally you need to change:

<TouchableWithoutFeedback onPress={this._onPressButton}>

to

<TouchableWithoutFeedback onPress={() => this._onPressButton()}>

Comments

1

Your onPressButton method is not bound to the context, and as the above answers mentioned you need to use this.setState({ rate: 1 });.

You could either add a constructor and use .bind(this) like below:

constructor(props) {
    super(props);

    this. _onPressButton = this. _onPressButton.bind(this)
  }

Or you could use an auto-binding arrow function like below:

_onPressButton = () => {
    Alert.alert('You tapped the button!')
    this.setState({ rate: 1 });
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.