Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...