Javascript React Native-从作为prop传递的函数调用方法
我正在使用一个名为的库,该库提供了一个选项来覆盖名为renderButton()的方法,以创建触发登录过程的按钮 现在我需要调用一个在LinkedInModal类中打开的方法,这个类与接收renderButton作为prop的类相同 如何从renderButton方法调用此“open”方法?我已尝试:Javascript React Native-从作为prop传递的函数调用方法,javascript,react-native,Javascript,React Native,我正在使用一个名为的库,该库提供了一个选项来覆盖名为renderButton()的方法,以创建触发登录过程的按钮 现在我需要调用一个在LinkedInModal类中打开的方法,这个类与接收renderButton作为prop的类相同 如何从renderButton方法调用此“open”方法?我已尝试: LinkedInModal.open() 方法如下所示: renderButton = () => { return (React.createElement(Toucha
LinkedInModal.open()
方法如下所示:
renderButton = () => {
return (React.createElement(TouchableOpacity,
{ accessibilityComponentType: 'button', accessibilityTraits: ['button'],
onPress: LinkedInModal.open()
},
React.createElement(Text, {style: {color: "#FFF"}}, "Continue with Linkedin")));
}
并将其传递给组件,如下所示:
<LinkedInModal
clientID="..."
clientSecret="..."
redirectUri="https://www.linkedin.com/developer/apps"
onSuccess={token => this.linkedinLogin(token.access_token)}
linkText="Continue with Linkedin"
renderButton={this.renderButton}
/>
您似乎没有正确包装
文本。
你能试试吗
renderButton = () => (
<TouchableOpacity
accessibilityComponentType="button"
accessibilityTraits={['button']}
onPress={() => LinkedInModal.open()}>
<Text style={{color: "#FFF"}}> Continue with Linkedin </Text>
</TouchableOpacity>
)
renderButton=()=>(
LinkedInModal.open()}>
继续使用Linkedin
)
解决方案是创建一个参考:
linkedRef = React.createRef();
然后在调用组件时,将其作为道具传递:
<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}
/>
感谢您的回答,我得到了相同的错误,函数没有定义,我认为从LinkedinModal调用方法时遗漏了一些东西。我在问题上加了一个错误。
<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}
/>
onPress={() => this.linkedRef.current.open()}>