[英]React Native - Call method from a function passed as prop
I am working with a library called react-native-linkedin , this library gives an option to override a method called renderButton(), to create the button which triggers login process.我正在使用一个名为react-native-linkedin的库,该库提供了一个选项来覆盖名为 renderButton() 的方法,以创建触发登录过程的按钮。
Now I need to call a method open inside class LinkedInModal, this class is the same that receives renderButton as prop.现在我需要在 class LinkedInModal 中调用 open 方法,这个 class 与接收 renderButton 作为 prop 的方法相同。
How can I call this "open" method from my renderButton method?, I have tried:如何从我的 renderButton 方法调用此“打开”方法?,我尝试过:
LinkedInModal.open()
Method looks like:方法如下:
renderButton = () => {
return (React.createElement(TouchableOpacity,
{ accessibilityComponentType: 'button', accessibilityTraits: ['button'],
onPress: LinkedInModal.open()
},
React.createElement(Text, {style: {color: "#FFF"}}, "Continue with Linkedin")));
}
And it is passed to the component as:并将其传递给组件:
<LinkedInModal
clientID="..."
clientSecret="..."
redirectUri="https://www.linkedin.com/developer/apps"
onSuccess={token => this.linkedinLogin(token.access_token)}
linkText="Continue with Linkedin"
renderButton={this.renderButton}
/>
But it does not work.但它不起作用。
Error I get is:我得到的错误是:
TypeError: _reactNativeLinkedin.default.open is not a function.
(In '_reactNativeLinkedin.default.open()', '_reactNativeLinkedin.default.open' is undefined)
Solution is to create a reference:解决方案是创建一个引用:
linkedRef = React.createRef();
then when calling the component, pass it as prop:然后在调用组件时,将其作为 prop 传递:
<LinkedInModal
ref={this.linkedRef}
clientID="..."
clientSecret="..."
redirectUri="https://www.linkedin.com/developer/apps"
onSuccess={token => this.linkedinLogin(token.access_token)}
linkText="Continue with Linkedin"
renderButton={this.renderButton}
/>
And in my custom method use it:在我的自定义方法中使用它:
onPress={() => this.linkedRef.current.open()}>
It seems that you are not wrapping the Text
correctly.您似乎没有正确包装Text
。 Can you try:你能试一下吗:
renderButton = () => (
<TouchableOpacity
accessibilityComponentType="button"
accessibilityTraits={['button']}
onPress={() => LinkedInModal.open()}>
<Text style={{color: "#FFF"}}> Continue with Linkedin </Text>
</TouchableOpacity>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.