Can't type in React input text field

后端 未结 8 2203
无人共我
无人共我 2020-12-02 11:56

I\'m trying my first bit of React.js and am stumped early on... I have the code below, which renders a search form into

. B
相关标签:
8条回答
  • 2020-12-02 12:08

    In a class component context...

    If the changeHandler method is a normal function:

    handleChange(e){
        this.setState({[e.target.name]:[e.target.value]});
    }
    

    it can be used such as this...onChange={(e)=>this.handleChange(e)}

    <input type="text" name="any" value={this.state.any} onChange={(e)=>this.handleChange(e)}></input>
    

    If the changeHandler method is an arrow function:

    handle = (e) =>{
            this.setState({[e.target.name]:[e.target.value]});
        }
    

    it can be used like this... onChange={this.handle}

     <input type="text" name="any2" value={this.state.any2} onChange={this.handle} ></input>
    

    And this solved my "Can't type in React input text field" problem.

    0 讨论(0)
  • 2020-12-02 12:18

    i'm also facing that problem now solved.Give the onChange to the searchTool. then that problem will solve for you.

    0 讨论(0)
  • 2020-12-02 12:27

    For me the following simple change worked perfectly

    <input type="text" 
            value={props.letter} 
            onChange={event => setTxtLetter(event.target.value)} /> {/* does not work */}
    

    change... value={myPropVal} to... defaultValue={myPropVal}

    <input type="text" 
            defaultValue={props.letter} 
            onChange={event => setTxtLetter(event.target.value)} /> {/* Works!! */}
    
    0 讨论(0)
  • 2020-12-02 12:28

    You haven't properly cased your onchange prop in the input. It needs to be onChange in JSX.

    <input
      type="text"
      value={this.props.searchString}
      ref="searchStringInput"
      onchange={this.handleChange} <--[should be onChange]
    />  
    

    The topic of passing a value prop to an <input>, and then somehow changing the value passed in response to user interaction using an onChange handler is pretty well-considered in the docs.

    They refer to such inputs as Controlled Components, and refer to inputs that instead let the DOM natively handle the input's value and subsequent changes from the user as Uncontrolled Components.

    Whenever you set the value prop of an input to some variable, you have a Controlled Component. This means you must change the value of the variable by some programmatic means or else the input will always hold that value and will never change, even when you type -- the native behaviour of the input, to update its value on typing, is overridden by React here.

    So, you're correctly taking that variable from state, and have a handler to update the state all set up fine. The problem was because you have onchange and not the correct onChange the handler was never being called and so the value was never being updated when you type into the input. When you do use onChange the handler is called, the value is updated when you type, and you see your changes.

    0 讨论(0)
  • 2020-12-02 12:28

    Using value={whatever} will make it so you cannot type in the input field. You should use defaultValue="Hello!".

    See https://facebook.github.io/react/docs/uncontrolled-components.html#default-values

    Also, the onchange should be onChange as @davnicwil points out.

    0 讨论(0)
  • 2020-12-02 12:28

    This might be caused by the onChange function is not updating the proper value which is mentioned in the input.

    Example:

    <input type="text" value={this.state.textValue} onChange = {this.changeText}></input>
    
     changeText(event){
            this.setState(
                {textValue : event.target.value}
            );
        }
    

    in the onChange function update the mentioned value field.

    0 讨论(0)
提交回复
热议问题