2017-07-22 12 views
3

React NativeのRedux AddTodoの例を使用して作業しています。以下の最初のAddTodoの例では、Stateを使用してTextInput値を保存しています。Clear ReactネイティブTextInput

class AddTodo extends React.Component{ 

    constructor(props){ 
     super(props); 
     this.state = { todoText: "" }; 
    } 
    update(e){ 
     if(this.state.todoText.trim()) this.props.dispatch(addTodo(this.state.todoText)); 
     this.setState({todoText: "" }); 
    } 
    render(){ 
     return(
      <TextInput 
       value = {this.state.todoText} 
       onSubmitEditing = { (e)=> { this.update(e); } } 
       onChangeText = { (text) => {this.setState({todoText: text}) } } /> 
     ); 
    } 
} 

しかしReduxの例をいくつか以下、次のコードは非常に短く、またTextInputvalue

let AddTodo = ({ dispatch }) => { 

    return (
     <TextInput 
      onSubmitEditing = { e => { dispatch(addTodo(e.nativeEvent.text)) } } 
     /> 
) 
} 

を提出した後にクリアされていないことを除いて動作します私はクリアできる方法はありますonSubmitEditingのInputText値?

答えて

12

は、例えば、あなたのTextInputに参照を追加します。

<TextInput ref={input => { this.textInput = input }} /> 

を、その後

+0

ニースをご入力値をクリアするthis.textInput.clear()を呼び出します。ありがとうございました。 –

関連する問題