javascript - 如何在React Native中将State View样式宽度设置为+1,高度设置为+1?

标签 javascript react-native

我想要这个框( View )的样式宽度和高度+1

我不写 style={{width:{{this.state.w}},height{{this.state.h}},backgroundColor:'blue'}}

export default class App extends Component { 

constructor(props){ 
 super(props); 
  this.state = { 
    w: 50, 
    h: 50, 
  } 
 } 

_addBox(){ 

  this.setState ({ 
      w:this.state.w + 1, 
      h:this.state.h + 1 
  });
}


 render() { 
  return ( 
  <view> 
   <view style="{{{styles.box}}}"> 
    <view style{{width:xxxx,height:xxxx,backgroundcolor:'blue'}}> 
     <text style="{styles.box}">
      {this.state.w}
     </text> 
    </view> 
    <buttontitle="add box"="" onpress="{this._addBox.bind(this)}" /> 
   </view> 
  </view> 
);
} 
}


帮帮我:(

最佳答案

如果你在setState中使用状态那么推荐的方式是:

_addBox = () => {
    this.setState(prevState => ({
        w: prevState.w + 1,
        h: prevState.h + 1
    }))
}

在你的渲染函数中只需使用你的状态:

render() {
    const { w, h } = this.state
    return (
        <View>
            <View style={styles.box}>
                <View style={{ width: w, height: h, backgroundColor: 'blue' }}>
                    <Text style={styles.box}>{w}</Text>
                </View>
                <Button title="add box" onPress="{this._addBox}" />
            </View>
        </View>
    )
}

关于javascript - 如何在React Native中将State View样式宽度设置为+1,高度设置为+1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58744409/

相关文章:

javascript - 如何在 JavaScript 中始终获取最外层元素

javascript - 如何在 Safari 中单击输入文本字段时突出显示?

javascript - 使用 ajax 请求下载文件

javascript - 在图表内显示标签 - angular.js 和 chart.js

react-native - 如何在 React Native 中将 TextInput 放在屏幕底部?

javascript - React Native 链接短信

reactjs - 如何将状态传递给 React Native 组件

javascript - "Script"范围下的类别

react-native - 从 Expo App 调用本地托管的服务器

ios - 在 Xcode 存档期间找不到 libCodePush.a