小编Mat*_*zcz的帖子

在反应本机样式表中使用变量将无法识别该变量

我将颜色作为 props.color 导入到我的功能组件中,并将其设置为状态“tagColor”。当我在样式表中使用 tagColor 作为值来设置背景颜色时,我收到错误“找不到变量 tagColor”

如何在样式表中使用变量?

const Tag = (props) => {
  
  const [tagColor, setColor] = useState(props.color)

  return (
    <View style={styles.container}>
      <TouchableOpacity style={styles.tag} title='tag'>
           
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    alignItems: "center",
    justifyContent: "center",
    height: 25,
    display: 'flex'
  },
  tag: {
    backgroundColor: tagColor,  
}
});

export default Tag;
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-native react-native-stylesheet

4
推荐指数
1
解决办法
2967
查看次数