我有下面的代码:为什么我不能使用“this”在类组件中使用mystyle?
import React, { Component } from 'react';
import { Text, view } from 'react-native';
class LoginForm extends Component {
render() {
return(
<Text style={ styles.errorMessageStyle }>
{this.props.auth.error}
</Text>
);
}
}
const styles = {
errorMessageStyle: {
fondSize: 20,
alignSelf: 'center'
}
};
为什么当我在style={ this.styles.errorMessageStyle }
使用this
它让我错误Cannot read property 'errorMessageStyle' of undefind
。 但是当我删除它的作品?
因为你不明白这是什么意思。 'this'指向当前实例,但'styles'在您的模块中声明为const,并且不会绑定到任何实例 – Dummy
@Dummy您可以添加更多细节并将其作为答案发布吗? – farmcommand2