2

Hi I want to clear the text after submitting the val in the Text Input in the react-native code Here is my code

export default function AddTodo({onSubmit}) {
    const [text, setText] = useState('')
    const changeHandler = (val) => {
        setText(val)
    }
    return (
        <View>
            <TextInput
                style={styles.newInput}
                placeholder="Add task"
                onChangeText={changeHandler}
            />
            <Button onPress={() => onSubmit(text)} title="Add Task" color='#1881e9' />
        </View>
    )
}

5 Answers 5

4

Add a value attribute to your TextInput like this

<TextInput
                style={styles.newInput}
                placeholder="Add task"
                onChangeText={changeHandler}
                value = {text}
            />

And then clear the text when on submit like this

  <Button onPress={() => {
            onSubmit(text)
            setText('') }} title="Add Task" color='#1881e9' />

Total code will be something like this:

import React from 'react';
import {View,Text, TextInput, Button} from 'react-native';
import  {useState} from 'react';
export default function AddTodo({onSubmit}) {
    const [text, setText] = useState('')
    const changeHandler = (val) => {
        console.log("val is",val);
        setText(val)
    }

    return (
        <View style={{marginTop:100}}>
            <TextInput
                placeholder="Add task"
                onChangeText={changeHandler}
                value={text}
            />
            <Button onPress={() => {
                onSubmit(text)
                setText('') }} title="Add Task" color='#1881e9' />
        </View>
    )
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a code snippet for your problem with the solution.

export default function AddTodo({onSubmit}) {
const [text, setText] = useState('')
const changeHandler = (val) => {
    setText(val)
}

const handleSumit = () => {
    // perform the summit operation
    setText("");
}
return (
    <View>
        <TextInput
            style={styles.newInput}
            placeholder="Add task"
            onChangeText={changeHandler}
        />
        <Button onPress={handleSumit} title="Add Task" color='#1881e9' />
    </View>
)

}

Comments

1

Call no arg onSubmit()

<Button onPress={() => onSubmitAndClear(text)} title="Add Task" color='#1881e9' />

    const onSubmitAndClear() = (text) => {
            setText(text)
         /// after submit operation then clear

          setText('')
        }

1 Comment

check the syntax, there are some issues.
1

you can use ref of your TextInput , and assign your text directly it like

refOfTextInput.setNativeProps({ text: "hello world" })

to the get the ref of TextInput you can use this approach

<TextInput ref={(ref) => { this.refOfTextInput = ref; }} />

so inside your text callback or any other component if you have ref of your TextInput you can use it like

        const onSubmitAndClear() = (text) => {
            refOfTextInput.setNativeProps({ text: "hello world" })
        }

Comments

0

You can try this:

add this function:

const clear = () {
  setText('')
}

and then:

<Button onPress={() => {onSubmit(text), clear()} } title="Add Task" color='#1881e9' />

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.