0

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 />
        </>
    )
}
2
  • 1
    That's because you are creating a global variable. You must be using sloppy mode (as opposed to strict). Change your code const onPressed = () => alert('Clicked A')and you'll get the expected behavior. Commented Sep 22, 2023 at 6:40
  • @RuanMendes Yes, it works. Thanks! Commented Sep 22, 2023 at 11:37

2 Answers 2

-1

You can do it both ways, either it's a separate function in each or it should be a common function

With Separate:

    const SmallComponentA = ({}) => {
        const onPressed = () => alert('Clicked A')
    
        return <Text onPress={onPressed} >Press On A</Text>
    }
    
    const SmallComponentB = ({}) => {
        const onPressed = () => alert('Clicked B')
    
        return <Text onPress={onPressed} >Press On B</Text>
    }
    
    const ActualComponent = ({}) => {
        return(
            <>
            <SmallComponentA />
            <SmallComponentB />
            </>
        )
    }

Common Function:

    const onPressed = ( component ) => alert('Common Clicked '+ component);
    
    const SmallComponentA = ({}) => {
      return <Text onPress={()=>onPressed("A")}>Press On A</Text>;
    };
    
    const SmallComponentB = ({}) => {
      return <Text onPress={()=>onPressed("B")}>Press On B</Text>;
    };
    
    const ActualComponent = ({}) => {
      return (
        <>
          <SmallComponentA />
          <SmallComponentB />
        </>
      );
    };

Hope it will hep you!

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

1 Comment

Yes, by adding a const keyword it works fine. For your second approach - We should avoid an arrow function inside the render method because the arrow function returns a new function every time. This causes React to think something has changed in our view, when in fact nothing has.
-1

So you can individually set the onPressed function inside each of the individual arrow function like,

const SmallComponentA = ({}) => {
    const onPressed = () => alert('Clicked A');

    return <Text onPress={onPressed}>Press On A</Text>;
}

const SmallComponentB = ({}) => {
    const onPressed = () => alert('Clicked B');

    return <Text onPress={onPressed}>Press On B</Text>;
}

const ActualComponent = ({}) => {
    return (
        <>
            <SmallComponentA />
            <SmallComponentB />
        </>
    );
}

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.