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

131
Vistas
setVariable of useState() is not giving accurate results

Please refer the image

When I change the content of input field and log it using console.log(e.target.value) then updated value gets logged but when i use setContent(e.target.value) it is setting the content's value to previous value of input

const Search=()=>{
    const[content,setContent]=useState([]);
    const[todos,setTodos]=useState([]);

    useEffect(() =>{
        document.title="Search";
    },[])



    //searching in database
    const searchDatabase=(content)=>{
        axios.get(`${base_url}/search/${content}`).then(
            (response)=>{
                setTodos(response.data);
            },
            ()=>{
                console.log("Database Down")
            }
        )
    }

return (
        <div>
            <Row>
                <Form className="form-inline mt-2 mb-4">
                    <FaSearch/>
                    <Input id="searchBox" className="ml-3" type="text"  
                    onChange={(e)=>{
                        console.log("current value: "+e.target.value);

                        setContent(e.target.value);

                        console.log("content value: "+content);

                        searchDatabase(content) 
                    }}
                    />


                </Form>
            </Row>
        </div>
       )
}

Thanks for help..

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

add function onChange event and use useEffect for do something on change of your content

const Search=()=>{

    const [content,setContent]=useState([]);
    const [todos,setTodos]=useState([]);


   const onSearchChange = (e) => {
    let inputUpdated = {
      ...content,
      [e.target.name]: e.target.value,
    };
    setContent(Updated);

    // check with console.log
    console.log(inputUpdated);
  };


    //searching in database
    const searchDatabase=(content)=>{
        axios.get(`${base_url}/search/${content}`).then(
            (response)=>{
                setTodos(response.data);
            },
            ()=>{
                console.log("Database Down")
            }
        )
    }



    useEffect(() =>{
        document.title="Search";
    },[ ])


    // On Content Change
    useEffect(() =>{
        searchDatabase(content); //Do as you want
    },[setContent])




return (
        <div>
            <Row>
                <Form className="form-inline mt-2 mb-4">
                    <FaSearch/>
                    <Input id="searchBox" className="ml-3" type="text"  
                           onChange={onSearchChange}
                    />

                </Form>
            </Row>
        </div>
       )
}
about 4 years ago · Juan Pablo Isaza Denunciar
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