最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ReactJS OnKeyUp event not getting value on controlled input - Stack Overflow

matteradmin9PV0评论

I'm building a ReactJS search ponent for data filtering through search.

The idea is that the user types a word, letter after letter, and the system will filter all registers containing that word. The basic ponent is detailed below:

class SearchInput extends Component {
    static propTypes = {
        onKeyUp: PropTypes.func,
        placeHolder: PropTypes.string,
        value: PropTypes.string
    };

state = {
    searchText: ""
};

handleKeyUp = event => {

    console.log(event.target.value) // <== No result. Always empty

    let newSearchText = event.target.value;
    this.setState({ searchText: newSearchText });
    if (this.props.onKeyUp) this.props.onKeyUp(newSearchText);
};

render() {
    console.log(this.state.searchText) // <== Always empty

    return (
        <div className="search-input">
            <div className="search-input-icon">
                <Icon name="faSearch" />
            </div>
            <input
                autoFocus="true" 
                type="text"
                onKeyUp={this.handleKeyUp}
                placeholder={this.props.placeHolder}
                value={this.state.searchText}
            />
        </div>
    );
}

I'm not getting the key pressed value on the handleKeyUp event handler.

It works if I ommit the value={this.state.searchText} (uncontrolled) from the code, but I need a way to set the searchText from outside the ponent (initialization, other ponent selection, etc.).

Why am I not getting the event.target.value data on my handler? How to fix it?

I'm building a ReactJS search ponent for data filtering through search.

The idea is that the user types a word, letter after letter, and the system will filter all registers containing that word. The basic ponent is detailed below:

class SearchInput extends Component {
    static propTypes = {
        onKeyUp: PropTypes.func,
        placeHolder: PropTypes.string,
        value: PropTypes.string
    };

state = {
    searchText: ""
};

handleKeyUp = event => {

    console.log(event.target.value) // <== No result. Always empty

    let newSearchText = event.target.value;
    this.setState({ searchText: newSearchText });
    if (this.props.onKeyUp) this.props.onKeyUp(newSearchText);
};

render() {
    console.log(this.state.searchText) // <== Always empty

    return (
        <div className="search-input">
            <div className="search-input-icon">
                <Icon name="faSearch" />
            </div>
            <input
                autoFocus="true" 
                type="text"
                onKeyUp={this.handleKeyUp}
                placeholder={this.props.placeHolder}
                value={this.state.searchText}
            />
        </div>
    );
}

I'm not getting the key pressed value on the handleKeyUp event handler.

It works if I ommit the value={this.state.searchText} (uncontrolled) from the code, but I need a way to set the searchText from outside the ponent (initialization, other ponent selection, etc.).

Why am I not getting the event.target.value data on my handler? How to fix it?

Share Improve this question edited Apr 22, 2019 at 22:33 Muath 4,42712 gold badges44 silver badges70 bronze badges asked Apr 22, 2019 at 22:28 MendesMendes 18.6k38 gold badges166 silver badges282 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 2

I'm pretty sure you have to listen to the onChange event on an input field to get the updated target value. simply change

<input onKeyUp={this.handleKeyUp} />

to

<input onChange={this.handleKeyUp} />

Try to use event.key instead.

The event.target.value just points to your this.state.searchText which hasn't been set yet.

seems you forgot to bind the function on the constructor:

class SearchInput extends Component {
  constructor(props) {
    super(props);
    this.handleKeyUp = this.handleKeyUp.bind(this);
  }

  //... any code here

  handleKeyUp = event => {
    console.log(event.target.value);
  }

  render() {
    //... any code here

    <input
      autoFocus="true" 
      type="text"
      onKeyUp={this.handleKeyUp}
      placeholder={this.props.placeHolder}
      value={this.state.searchText}
    />
  }
}

Use this:

let newSearchText = event.target.getAttribute('value')
Post a comment

comment list (0)

  1. No comments so far