samius polis
samius polis

Reputation: 379

ReactJS onChange inputfield while typing?

I want to execute a additional function in ReactJS onChange in input type text field, then render function is:

render(){
    return (
        <div>
            <InputText />
        </div>
    )
}

The InputText component is:

class InputText extends React.Component {

    onChange(e){
        console.log("you typed again...");
    }

    render() {
        return (
            <input type="text" placeholder="search..." onChange={this.onChange()} />
        );
    }
}

The console log is to execute while user is typing but with my method the console log comes only if page is loaded.

What I have to fix there to get it runing if user start with typing?

Upvotes: 0

Views: 452

Answers (2)

Brett DeWoody
Brett DeWoody

Reputation: 62763

Instead of calling the function within the onChange prop, like:

onChange={this.onChange()}

you only need to pass the function, like:

onChange={this.onChange}

Upvotes: 1

Oen44
Oen44

Reputation: 3206

Remove () from this.onChange(). That's all.

Upvotes: 0

Related Questions