У меня есть собственный вид реакции, который я хочу динамически стилизовать.
Значение reaction будет получено из API, поэтому я хочу передать его в свою таблицу стилей.
const Weather = ({ reaction, temperature }) => {
//const bg = `weatherconditions.${reaction}.color`
return (
<View
style = {{ backgroundColor: weatherConditions[reaction].color }}>
Таблица стилей выглядит так
export const weatherConditions = {
Rain: {
color: '#005BEA',
title: 'Raining',
subtitle: 'Get a cup of coffee',
icon: 'weather-rainy'
},
Clear: {
color: '#f7b733',
title: 'So Sunny',
subtitle: 'It is hurting my eyes',
icon: 'weather-sunny'
},
Thunderstorm: {
color: '#616161',
title: 'A Storm is coming',
subtitle: 'Because Gods are angry',
icon: 'weather-lightning'
},
Clouds: {
color: '#1F1C2C',
title: 'Clouds',
subtitle: 'Everywhere',
icon: 'weather-cloudy'
},
Snow: {
color: '#00d2ff',
title: 'Snow',
subtitle: 'Get out and build a snowman for me',
icon: 'weather-snowy'
},
}
где Clear, Rain, ThunderStorm может быть значением reaction
Я хочу динамически предоставлять значение reaction.
я пытался сделать это
const Weather = ({ reaction, temperature }) => {
const bg = `weatherconditions.${reaction}.color`;
return (
<View
style = {{ backgroundColor: bg }}
>
и
<View
style = {{ backgroundColor: ${bg }}>
Но ни один из них, кажется, не работает. Любая помощь в решении этой проблемы будет оценена по достоинству.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Не уверен, что это то, что вы имеете в виду, но надеюсь, что это поможет.
const styles = {
weather = reaction => ({
backgroundColor: reaction
})
}
А затем в своем теге <View/> укажите реакцию
...
<View style = {StyleSheet.flatten([styles.weather(reaction)])}>
//Your code here
</View>
Чтобы получить запись объекта динамически, вы можете попробовать
object[String(outerKey)].innerKeyили классObject. напримерObject.entries(object), и это вернет пару данных,key:value, и получит доступ к значению по ключу.const color = Object.values(weatherConditions[reaction]).color