Я использую хуки, чтобы написать собственное приложение для реагирования. У меня проблема с использованием состояний внутри стилей. Фон текстового контейнера по умолчанию красный, а после нажатия кнопки «Подтвердить» он должен измениться на зеленый. На данный момент я сталкиваюсь с ошибкой, когда использую activeBtn в качестве backgroundColor в стиле. Пожалуйста, помогите мне изменить мой код правильным образом. Я упростил свой код, чтобы он был более понятным, как показано ниже:
import React, { useState } from "react";
import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
const DifficultScreen = (props) => {
const [activeBtn, setActiveBtn] = useState("red");
const confirmHandler = () => {
setActiveBtn("green");
};
return (
<View>
<View style={styles.container}>
<Text style={styles.title}>Difficult screen is showing</Text>
</View>
<View>
<TouchableOpacity onPress={confirmHandler} style={styles.btn}>
<Text>Confirm</Text>
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: activeBtn,
alignItems: "center",
justifyContent: "center",
width: "100%",
height: 90,
padding: 35,
},
title: {
color: "black",
fontSize: 18,
},
btn: {
color: "black",
padding: "10%",
backgroundColor: "white",
borderRadius: "5px",
alignSelf: "center",
textAlign: "center",
margin: "5%",
},
});
export default DifficultScreen;