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

242
Vistas
How to Change pre filled input values

I new to React. I have two input fields which are filled when the pages loads via axios but when I want to change the Values I am unable to change them even I can not type anything on them.

function MainView() {
    const [InputFields, setInputFields] = useState({
        name: "",
        fullURL: "",
    });

    const changeHandler = (e) => {
        setInputFields({
            ...InputFields,
            [e.target.name]: e.target.value,
        });
    };

    const [links, setLinks] = useState([]);
    const getLinks = () => {
        axios.get('../sanctum/csrf-cookie').then(response => {
            axios.get("/userlinks/getdata").then((res) => {
                console.log(res);
                setLinks(res.data);
            }).catch((err) => {
                console.log(err);
            });
        });
    };

    useEffect(() => {
        getLinks();
    }, []);

    return (<div>
            {links.map((link) => {
                return (<div className="card" key={link.id}>
                    <form>
                     <input className="form-control" value={link.name} type="text" name="name" placeholder="name" onChange={changeHandler} />
                    <input className="form-control" value={link.fullURL} type="text" name="fullURL" placeholder="fullURL" onChange={changeHandler} />
                    </form>
                </div>);
            })}
    </div>
    );
}

export default MainView;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This happens because in your onChange method, you are changing InputFields variable, where as your getLinks method changes links variable, which is being rendered on the screen.

If you want to set an initial value, and then allow the user to change it, change your input to :

<input className="form-control" defaultValue={link.name} 
 value={InputFields.name} type="text" name="name" placeholder="name" onChange={changeHandler} />

Likewise change for your other input, if you do not want the user to change the value later on, it's often better to add disable in the input to avoid confusing people. 🙂

I know that this has been done so that you can create a minimal reproducible example for us, but I would have directly called setInputFields in the axios.get section to avoid this problem in the first place, however, if not possible, use the defaultValue and value as I've shown above.

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