我刚开始学习 React Native 技术,所以我尝试在教程代码中更改一些行。这是一个添加新标题的表单,但如果 value === "",我想更改按钮的颜色。我试图找到,但大多数示例使用类,在这个项目中我想使用函数
import React, { useState } from 'react'
import { View, StyleSheet, TextInput, Button, Alert } from 'react-native'
export const AddTodo = ({ onSubmit }) => {
const [value, setValue] = useState('')
const pressHandler = () => {
if (value.trim()) {
onSubmit(value)
setValue('')
} else {
}
}
return (
<View style={styles.block}>
<TextInput
style={styles.input}
onChangeText={setValue}
value={value}
disabled
placeholder='Введите название дела...'
autoCorrect={false}
autoCapitalize='none'
/>
<Button title='Добавить' onPress={pressHandler} />
</View>
)
}
const styles = StyleSheet.create({
block: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 15
},
input: {
width: '70%',
padding: 10,
borderStyle: 'solid',
borderBottomWidth: 2,
borderBottomColor: '#3949ab'
},
button: {
color: 'red'
}
})