Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Can we change the type of useState variables at runtime?

I'm working on small react project where trainer used below code

created states using useState hooks

const [purchasePrice, setPurchasePrice] = useState("");
    const [downPayment, setDownPayment] = useState("");
    const [loanTerm, setLoanTerm] = useState("");
    const [loanApr, setLoanApr] = useState("");
    const [monthlyPayments, setMonthlyPayments] = useState(0.0);

Then passed those to afunction which returns true or false and also updates the state

 //Validate fields
        const validatedPrice = await validateField(purchasePrice, setPurchasePrice);
        const validatedPayment = await validateField(downPayment, setDownPayment);
        const validatedLoanTerm = await validateField(loanTerm, setLoanTerm);
        const validatedApr = await validateField(loanApr, setLoanApr);

code for validation :

const validateField = (field, setvalue) => {
        let int = parseFloat(field);
        if(field === "" || field === 0) {
            setvalue({...field.values, error: "Please enter a value"});
            return false;
        } else if(isNaN(int)) {
            setvalue({...field.values, error: "Please enter a number"});
            return false;
        } else {
            setvalue(int);
            return true;
        }
     };

in the validation function how did he add the object type to the state while the state initial type was string. 1.Can we change the type of useState initial value at runtime ? 2.what is .values in the line

setvalue({...field.values, error: "Please enter a value"});

is it a property/function of object.

Please find the full code here if you need full code to understand

https://github.com/sharathkumar111316/Mortgage-Calculator/blob/master/src/components/FormContainer.js

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda