Home > Mobile >  Call function from component
Call function from component

Time:10-23

I am writing app that uses react-native-immediate-phone-call and by pressing Pressable component I need to call function from that module.

import RNImmediatePhoneCall from 'react-native-immediate-phone-call';

export default function App() {
    const [getNumber, setNumber] = useState('');

    function makeCall() {
        console.log('dialing');
        return () => RNImmediatePhoneCall.immediatePhoneCall(getNumber);
    }
    ...
    return (
        ...
        <Pressable onPress={()=>makeCall()}>
            <Image source={require('./assets/phone-call.png')}/>
        </Pressable>
        ...
    );
}

Console log is displayed into terminal with Metro Bunler but return does not execute somehow. I am newbie into js and react. How function and function call should like?

CodePudding user response:

You are not calling the function but returning it so you get a function back.

Just change the following:

return () => RNImmediatePhoneCall.immediatePhoneCall(getNumber);

to:

RNImmediatePhoneCall.immediatePhoneCall(getNumber);

CodePudding user response:

At that moment getNumber == ''

So, I think RNImmediatePhoneCall.immediatePhoneCall('') do nothing.

Also, try with this (will be work when getNumber has a value)

function makeCall() {
    console.log('dialing');
    RNImmediatePhoneCall.immediatePhoneCall(getNumber);
}
  • Related