android - 类型错误:未定义不是对象(评估'style.inputStyle')
问题描述
我正在创建一个简单的身份验证表单。我在 TextInput 组件级别收到此错误。这与文本输入组件的样式有关。
这是代码:
const Input = ( {label, value, onChangeText} ) => {
const {inputStyle, labelStyle, containerStyle} = styles;
return(
<View style={containerStyle}>
<Text style={labelStyle}>{label}</Text>
<TextInput
style={inputStyle}
value ={value}
onChangeText = {onChangeText}
/>
</View>
);
const styles={
inputStyle:{
color: '#000',
paddingRight: 5,
paddingLeft: 5,
fontSize: 18,
lineHeight: 23,
flex: 2
},
labelStyle:{
fontSize: 18,
paddingLeft: 5,
flex: 1
},
containerStyle:{
height: 40,
flexDirection: 'row',
alignItems: 'center',
flex: 1
}
};
};
export {Input};
解决方案
保留styles
为全局变量。
const Input = ( {label, value, onChangeText} ) => {
const {inputStyle, labelStyle, containerStyle} = styles;
return(
<View style={containerStyle}>
<Text style={labelStyle}>{label}</Text>
<TextInput
style={inputStyle}
value ={value}
onChangeText = {onChangeText}
/>
</View>
);
};
const styles={
inputStyle:{
color: '#000',
paddingRight: 5,
paddingLeft: 5,
fontSize: 18,
lineHeight: 23,
flex: 2
},
labelStyle:{
fontSize: 18,
paddingLeft: 5,
flex: 1
},
containerStyle:{
height: 40,
flexDirection: 'row',
alignItems: 'center',
flex: 1
}
};
export {Input};
推荐阅读
- java - 如何在 Spring Boot 中捕获错误请求
- c# - c# 在visual studio中调试windows服务
- python - Django:找不到'create_order'的反向
- vb.net - vb.net 在类之间传递字符串
- python - 在数据框中连接多个 for 循环的更好方法
- jquery - 移动 3 级滚动问题
- javascript - Angular 集成 - HTTP 客户端
- html - 为什么 chrome 会覆盖 -webkit-appearance Css 属性?
- python - 使用多处理池调用具有参数化构造函数的类的方法
- linux - 如何将焦点放在xorg中的单个窗口?