我有下面的代碼:爲什麼我不能使用「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