首页 > 解决方案 > 如何在本机反应中将状态变量分配给样式组件?

问题描述

使用 react.js 可以将状态变量分配给 div 或任何其他元素,如下所示:

<div className={this.state.test}>

使用本机反应,我看不到如何实现相同的逻辑:

<Image style={this.state.test} /> 

本例中的测试变量 = 'styles.custom'

标签: react-native

解决方案


//imports
class App extends Component {
  state = {
    width: 64 //random property
  }
  render() {
   return <Image source={...} style={[styles.button, {...this.state}]}>
  }
}
const styles = StyleSheet.create{
 button: {
  height: 64
 }
}

推荐阅读