0

Can someone help me in figuring out wha is the best way to use setNativeProps in RN?.

In my react function component I did something like this

const AutoComplete = (props) => {
 let parentViewRef = null

  useEffect(() => {
    setTimeout(function(){ console.log(parentViewRef) }, 3000)
  },[])

  return(
    <View  
      ref={(viewRef) => { 
        parentViewRef = viewRef 
      }}
      style={styles.modelOpenViewMain}>    <View style={styles.modelOpenInputView}>

           </View>
        </View>
    )
  }
}

But unfortunately my parentViewRef is coming out to be undefined. Any idea what I could be doing wrong?

2 Answers 2

1

If I'm understanding correctly what you're trying to achieve, then you should be using useRef hook.

So your code would look like something like this:

import React, { useRef } from 'react';

const AutoComplete = (props) => {
 let parentViewRef = useRef(null);

  return(
    <View  
      ref={parentViewRef}
      style={styles.modelOpenViewMain}>    <View style={styles.modelOpenInputView}>

           </View>
        </View>
    )
  }
}

You will then be able to do whatever you want with your ref via its current property, e.g. parentViewRef.current. ...

Hope this helps!

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

Comments

0

Try Passing the parentViewRef as an argument to the useEffect method.

    const AutoComplete = (props) => {
    
    let parentViewRef = null

    useEffect(() => { 

           console.log(parentViewRef);

    }, [parentViewRef]);

    return (
        <View
            ref={(viewRef) => parentViewRef = viewRef}>
            <View >
                <Text>CONTENT TEXT</Text>
            </View>
        </View>
      )
    }

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.