Why can't I change my input value in React even with the onChange listener

前端 未结 6 1195
再見小時候
再見小時候 2021-02-05 00:20

I am quite new to React and after going through some tutorials, I was trying the below code of mine.

I made one component, passed props to it from a store, on compo

6条回答
  •  [愿得一人]
    2021-02-05 00:38

    If you would like to handle multiple inputs with one handler take a look at my approach where I'm using computed property to get value of the input based on it's name.

    import React, { useState } from "react";
    import "./style.css";
    
    export default function App() {
      const [state, setState] = useState({
        name: "John Doe",
        email: "john.doe@test.com"
      });
    
      const handleChange = e => {
        setState({
          [e.target.name]: e.target.value
        });
      };
    
      return (
        
    ); }

提交回复
热议问题