css - 如何在 native react 样式中设置动态值的 Prop

标签 css reactjs react-native

我正在从服务器获取数据,其中我必须在样式表中设置 Prop 才能动态更改背景颜色。但我收到错误

undefined is not an object (evaluating 'this.props.colorCode')

这是我的代码

class TopCategoryCard extends Component {
  render() {
    return (
      <View style={styles.container}>

        <View style={styles.CardMainView}>
        <View style={styles.ThirdLayerStyle}>
        </View>
        <View style={styles.SecondLayerStyle}>
         </View>
         <View style={styles.FirstLayerStyle}>
         <Image resizeMode={'contain'} style={styles.CatImageStyle}
          source={this.props.imageUri}
        />
         </View>
        <Text numberOfLines={1} style={{color:'#fff' ,  fontSize:13, top:28  , marginLeft:25  , marginRight:20, fontFamily: "poppinsregular"}}>{this.props.name}</Text>
       </View>

      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    padding:5
  },
  CardMainView:{
    flexDirection:'row',
    borderRadius:4,
    backgroundColor: this.props.colorCode,
    height:80,
    elevation:3,
    shadowColor:'#000',
    overflow:'hidden',
  }
  }

在上面的代码中,我成功获取了图像和名称,但在更改颜色代码时遇到错误,请帮助

变色代码

<ScrollView
                      style={{ marginTop:15 , marginLeft:-5  }}
                      horizontal={true}
                      showsHorizontalScrollIndicator={false}>
                      <FlatList 
                        data={this.state.data}
                        keyExtractor={(x, i) => i.toString()}
                        renderItem={({ item }) => (
                          <TouchableOpacity 
                            activeOpacity={0.9}
                            onPress={() =>
                              this.props.navigation.navigate(
                                "JobbyCategorylist",
                                { slug: item.slug }
                              )
                            }
                          >
                            <TopCategoryCard
                              imageUri={{ uri: `${item.url}` }}
                              name={`${entities.decode(item.name)}`}
                              colorCode={item.color}
                            />
                          </TouchableOpacity>
                        )}
                        horizontal={true}
                      />
                    </ScrollView>

最佳答案

您无法为 StyleSheet 属性动态分配值。相反,您可以尝试这样的事情:

<View style={[ styles.CardMainView, {backgroundColor: this.props.colorCode} ]}>

不要忘记从 CardMainView 中删除 backgroundColor: this.props.colorCode

关于css - 如何在 native react 样式中设置动态值的 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57653430/

相关文章:

css - 具有固定位置和可滚动内容的 ionic 可滚动选项卡

html - 在移动 Firefox 中隐藏地址栏

javascript - 为每个表格单元格随机分配不同的名称

javascript - 访问 React Table 中的过滤数据时出错

javascript - 在 react jsx 的一个元素中实现一组对象作为属性

javascript - 如何让 Luxon 的 Intl API 在 React-Native 中工作

jQuery 交换多个元素

reactjs - Safari 在每次渲染时以不同的顺序渲染数组元素

react-native - 无法从 bundle "index.android.bundle"加载脚本

javascript - 使用循环更改组件特征(样式)