0

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?

1
  • 1
    onChange={this.onChange}, without () Commented May 25, 2017 at 9:09

2 Answers 2

1

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

onChange={this.onChange()}

you only need to pass the function, like:

onChange={this.onChange}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.