Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

reactjs - Print text entered in TextInput to console

I want print the value which is entered in TextInput to console.

below is snippets from my code:

constructor(props) {
        super(props);
        this.state= {
            textValue: "",
        }
    }
    _textChange = (text) => {
        this.setState({
            textValue: text,
        })
        console.log(this.state.textValue);
}

AND:

<TextInput onChange={this._textChange.bind(this)}/>

I know that something is wrong with my code, but I am not able to figure it out. I just want to print the entered text in TextInput to be printed in console

Note

I have seen this answer. I am not able to understand the answers in the given link. I am very new to react-native. So please try to explain it the simple way.

question from:https://stackoverflow.com/questions/65642122/print-text-entered-in-textinput-to-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use onChangeText instead of onChange

<TextInput onChangeText={this._textChange}/>

if declare your method as a named function like below

    _textChange(text) {
        this.setState({
            textValue: text,
        })
     }

So you should use bind, because the this keyword will not refer to the component scope,but in your case it is not necessary.

Please take a look at this article to understand difference between named function and arrow function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...