How fix "`super()` is only valid inside a class constructor of a subclass"

Hi everyone, I need put this in a function:

constructor(props)
  {
    super(props);
    this.state={
      nombre:'',
      apellidos:'',
      alias:'',
    }
  }

  InsertRecord=()=>
  {
    var nombre=this.state.nombre;
    var apellidos=this.state.apellidos;
    var alias=this.state.alias;

    if(nombre.length==0 || apellidosnombre.length==0 || aliasnombre.length==0){
      alert("Required Field is Missing");
    }else{
      alert("fetch API code come here");
    }
  }

In the case of constructor is correct something like this?:

 const [nombre,setNombre] = useState("");
  const [apellidos,setApellidos] = useState("");
  const [alias,setAlias] = useState("");

const insertrecord = () =>
  {
    var nombre=this.state.nombre;
    var apellidos=this.state.apellidos;
    var alias=this.state.alias;

    if(nombre.length==0 || apellidos.length==0 || alias.length==0){
      alert("Required Field is Missing");
    }else{
      alert("fetch API code come here");
    }
  }


Please Help, I can’t convert my function in class because I would commit a lot of code that is already made especially for my function. Thanks.

Hi

Your question is more about JavaScript than Expo. Also, you say you need to do something, but it’s a weird question. Why do you need to put that in a function? You are asking the wrong question. Instead of saying how you want to accomplish some unspecified thing, rather say what you are trying to accomplish. After that you can say how you have tried to accomplish it, but you should start with what you’re trying to do.

If we know what you are trying to do we can suggest how to do it.
If you start with “I want to do it this way”, but we don’t know what it is that you are trying to do, then it’s hard to help you.

It looks like you want to store some things and then validate that those things contain something before calling some API. Is that about right?

The useState() stuff you have there look OK as long as you have all of that code inside a larger function. But you can’t use this outside of a class. Fortunately, you can just delete the following three lines:

Something like this:

allows you to just reference nombre directly. There is no need for this.state.nombre.

1 Like

Hi, thanks for taking the time to reply me. You’re right, my question here is somewhat out of place, I was a bit desperate because my progress was not working for me, I apologize.

I will explain you now, the first code, following this tutorial: User Registration with PHP and MYSQL in React Native || Insert Record Example - YouTube, in the minute 26:00, makes the code for insert a query in MySQL through the form, but that does it in a class, and I have a function, my code is similar to the code from the video but I can’t change the function for a class because I have a lot of code that is made for that function, I need convert the code of the class (the first code in my question, with the constructor) to a code for a function.

I do not know if I explained well. :pensive:

Thank you for having answered and interested in my question.