0

enter image description here

I am trying to add these 2 Functions in one. I am new to react-native. As in code, I tried but only the first one is working.

My question is How I can use setSearch and animation together in the onChangetext function?

<TextInput
    style={styles.searchbox}
    onChangeText={(text) => {
        setSearch(text)
        scaleValue.setValue(1)
        Animated.timing(scaleValue,{
            toValue:0.5,
            duration:100,
            easing: Easing.linear,
            useNativeDriver:true
        }).start();
     }}/>

3 Answers 3

1

The problem here is that once you use setSearch(text) you're changing the state of the component and that change trigger the re-rendering of the component itself so the rest of the code doesn't run.
I've never used react native so I'm not sure 100% but if you are using a Functional component you can do something like this

const [search, setSearch] = useState('')
useEffect(() => {
   scaleValue.setValue(1)
   Animated.timing(scaleValue,{
                   toValue:0.5,
                   duration:100,
                   easing: Easing.linear,
                   useNativeDriver:true
                }).start();
}, [search])


return (<TextInput
              style={styles.searchbox}
              onChangeText={(text) => setSearch(text) }/>)

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

Comments

0

Can't you just use an intermediate function to call them both?

<TextInput
    style={styles.searchbox}
    onChangeText={(text) => intermediateFunction(text)}
/>

intermediateFunction = (text) => {
    setSearch(text);
    Animated.timing(scaleValue,{
                  toValue:0.5,
                  duration:100,
                  easing: Easing.linear,
                  useNativeDriver:true
                }).start();
}

5 Comments

I'm assuming you're calling the intermediateFunction outside of the return body?
yes.but when I tried to run in return it gives error..
')' expexted ahead of <safeareaview>
Have you tried commenting out the 'safeAreaView' code to check that this is working and that error isn't due to something else?
0

Create a new function to encapsulate the two actions you want to invoke by onChangeText. Something along the lines of:

 const doSomething = (text) => {
          setSearch(text)
          scaleValue.setValue(1)
          Animated.timing(scaleValue,{
            toValue:0.5,
            duration:100,
            easing: Easing.linear,
            useNativeDriver:true
            }).start();
          }
 }

 ...

 <TextInput style={styles.searchbox}
          onChangeText={(text) => doSomething(text) />

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.