I am developing a react-native app. Recently, I encountered a strange behavior of functional components. Typically, I break large component into smaller components, write separate logic for each smaller component, and then call them in the same component.
In the below code, I have not defined the method onPressed in the SmallComponentB but when I click on this it calls the method from SmallComponentA
const SmallComponentA = ({}) => {
onPressed = () => alert('Clicked A')
return <Text onPress={onPressed} >Press On A</Text>
}
const SmallComponentB = ({}) => {
return <Text onPress={onPressed} >Press On B</Text>
}
const ActualComponent = ({}) => {
return(
<>
<SmallComponentA />
<SmallComponentB />
</>
)
}
const onPressed = () => alert('Clicked A')and you'll get the expected behavior.