将native,TextInput设置onChangeText作为函数

时间:2018-06-12 05:44:43

标签: react-native

我有以下组件,我试图将TextInput元素的onSubmitEditing函数设置为名为func的自定义函数。我希望它将TextInput框的内容作为函数的输入。如何才能做到这一点?以下是我尝试这样做的失败:

export default class Component4 extends Component {
    func(input){
        // will add stuff here later
    }

    render(){
        return (
            <View style={{padding: 30}}>
                <TextInput placeholder="default" onSubmitEditing=this.func/>
            </View>
        );
    }
}

PS:

感谢大家到目前为止的帮助,我已经设法让它部分工作,现在是我的代码:

export default class Component4 extends Component {
    constructor(props) {
        super(props);
        this.state = {thing: 'asdf'};
    }

    func(input){
        this.state.thing = input;
        // I will eventually do more complicated stuff here
    }

    render(){
        return (
            <View style={{padding: 30}}>
                <TextInput placeholder="default" onSubmitEditing={this.func}/>
                <Text>{this.state.thing}</Text>
            </View>
        );
    }
}

这给出了一个错误,我试图让它设置为state.thing 输入。感谢

2 个答案:

答案 0 :(得分:1)

选项1: <View style={{padding: 30}}> <TextInput placeholder="default" onSubmitEditing={this.func}/> </View>

选项2: <View style={{padding: 30}}> <TextInput placeholder="default" onSubmitEditing {(e)=>this.func(e.target.value)}/> </View>

答案 1 :(得分:0)

state={
         text:''
}
Func(e){
        text:e.target.value
 }
 onSubmitChange={this.func}

不要忘记绑定 func 功能