React native 在react native 0.60.5中,无法使用此“从内部渲染”方法调用类函数
无法在render方法中使用this.func调用类函数React native 在react native 0.60.5中,无法使用此“从内部渲染”方法调用类函数,react-native,React Native,无法在render方法中使用this.func调用类函数 type Props = {}; export default class App extends Component<Props> { func = () => { console.log('hello') } render() { return ( <View style={styles.container}> <Button
type Props = {};
export default class App extends Component<Props> {
func = () => {
console.log('hello')
}
render() {
return (
<View style={styles.container}>
<Button
title='logIn'
onPress={() => this.func()}
/>
</View>
);
}
}
type Props={};
导出默认类应用程序扩展组件{
func=()=>{
console.log('hello')
}
render(){
返回(
this.func()}
/>
);
}
}
如果func放在render方法中,那么onPress={()=>func()}可以正常工作。以前,这在其他项目中也适用。在构造函数本身中绑定func()方法
constructor(props) {
super(props);
this.state = {
};
this.func = this.func.bind(this);
}
你的功能
func(){
console.log('hello')
}
你的按钮看起来像这样
<Button
title='logIn'
onPress={this.func}
/>
因为这样你就不会每次结束创建引用状态了
更改和渲染时调用。在构造函数中绑定方法
它本身也将帮助您提高性能
我希望这有助于。。。。谢谢:)谢谢它成功了,这是react native的新变化吗?@vishwa它从一开始就存在。绑定函数而不是直接调用将在reactjs和ReactNative中的性能上产生巨大差异。你可以在谷歌上看到更多关于这方面的信息。谢谢:)