Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda