I have one component which is returning the Image and which Height is coming from api response
const Banner = () => {
const [AdData, setAdData] = useState();
const [showBanner, setShowBanner] = useState(false);
const [currentIndex, setCurrentIndex] = useState(0);
const imageHeight = MiscUtils.returnDefaultValueIfNotValid(0, AdData, 'fetchAds', 'ads', currentIndex, 'image', 'height');
return (
<Image
style={Styles.image}
resizeMode={'stretch'}
source= {{uri: imageUrl}}
height={imageHeight}
/>
)
}
export default Banner
I have one more component where in useEffect I need to use the imageHeight from Banner component file and instead of 65 I need imageHeight
const TabScreen = () => {
useEffect(() => {
if(ref.current && !isScrollOnTop(ref.current.getCurrentScrollOffset())) {
(isDisplayAdBannerVisible) ?
ref.current.scrollToOffset(0, ref.current.getCurrentScrollOffset() + 65, true) :
ref.current.scrollToOffset(0, ref.current.getCurrentScrollOffset() - 65, true)
}
})
}
export default React.memo(TabScreen)
Help me how to do this, I have tried it using the redux and created new file, but that’s not working.
2
Answers
Pass the callback function as a prop to the child
Parent Component
Child Component
Passing props from child component to parent component is not possible. You can either pass a callback function as prop which gets an height arg and call that inside your
Banner
component’suseEffect
function.Or you can just put that value in a global store (context, redux etc.) and read it there for other components that need that value. But for something like an image height. Passing a callback function and calling that is easier and less complex.