A
A
antonpanamo2021-12-28 23:51:23
React Native
antonpanamo, 2021-12-28 23:51:23

What is wrong with this code?

import Checkbox from 'expo-checkbox';
import React, { useState } from 'react';
import { StyleSheet, Text, View, Alert, Modal,  Pressable, TextInput, Switch } from 'react-native';

export default function App() {
  
   this.state = {
    counter: 0,
    title: 'setState tutorial',
  };

  
this.clickHandler = () => {
    this.setState({ counter: Math.random() });
  };
  return (

    
  <>

     <h1>{this.state.title}</h1>
        <div>{this.state.counter}</div>
        <button onClick={this.clickHandler}>Click!</button>
   
   </>


  );
}



const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginHorizontal: 16,
    marginVertical: 32,
  },
  section: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  paragraph: {
    fontSize: 15,
  },
  checkbox: {
    margin: 8,
  },
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2021-12-29
@mmmaaak

Is it because the functional component does not have this.setState?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question