Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!