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

前端 未结 6 1196
再見小時候
再見小時候 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:24

    Unlike in the case of Angular, in React.js you need to update the state manually. You can do something like this:

    <input
        className="form-control"
        type="text" value={this.state.name}
        id={'todoName' + this.props.id}
        onChange={e => this.onTodoChange(e.target.value)}
    />
    

    And then in the function:

    onTodoChange(value){
            this.setState({
                 name: value
            });
        }
    

    Also, you can set the initial state in the constructor of the component:

      constructor (props) {
        super(props);
        this.state = {
            updatable: false,
            name: props.name,
            status: props.status
        };
      }
    
    0 讨论(0)
  • 2021-02-05 00:25

    In react, state will not change until you do it by using this.setState({});. That is why your console message showing old values.

    0 讨论(0)
  • 2021-02-05 00:34

    You can do shortcut via inline function if you want to simply change the state variable without declaring a new function at top:

    <input type="text" onChange={e => this.setState({ text: e.target.value })}/>
    
    0 讨论(0)
  • 2021-02-05 00:34

    In React, the component will re-render (or update) only if the state or the prop changes.

    In your case you have to update the state immediately after the change so that the component will re-render with the updates state value.

    onTodoChange(event) {
            // update the state
        this.setState({name: event.target.value});
    }
    
    0 讨论(0)
  • 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 (
        <div>
          <input
            type="text"
            className="name"
            name="name"
            value={state.name}
            onChange={handleChange}
          />
    
          <input
            type="text"
            className="email"
            name="email"
            value={state.email}
            onChange={handleChange}
          />
        </div>
      );
    }
    
    0 讨论(0)
  • 2021-02-05 00:39

    I think it is best way for you.

    You should add this: this.onTodoChange = this.onTodoChange.bind(this).

    And your function has event param(e), and get value:

    componentWillMount(){
            this.setState({
                updatable : false,
                name : this.props.name,
                status : this.props.status
            });
        this.onTodoChange = this.onTodoChange.bind(this)
        }
        
        
    <input className="form-control" type="text" value={this.state.name} id={'todoName' + this.props.id} onChange={this.onTodoChange}/>
    
    onTodoChange(e){
             const {name, value} = e.target;
            this.setState({[name]: value});
       
    }
    
    0 讨论(0)
提交回复
热议问题