动态反应设置状态属性 [英] React set state property dynamically

查看:62
本文介绍了动态反应设置状态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用react,并且我有一些方法可以分别设置COmponent的状态. 我有以下方法:

I'm using react and I have some methods to set the state of my COmponent separately. I have the following methods:

setLineColor(value){
  this.setState({stroke:value},()=>{
  this.props.data(this.getStyleData());
 });
}
setFillColor(value){
 this.setState({ fill:value},()=>{
 this.props.data(this.getStyleData());
 });
}
setMode(value){
 this.setState({ mode:value},()=>{
 this.props.data(this.getStyleData());
 });
}

我如何组合这些方法,这样我就可以拥有类似的东西:

How can I combine the methods, so that I can have something like:

setAttribute(propery,value){...}

?

推荐答案

类似

setAttribute(property, value) { 
  this.setState({ [property]: value }, () => {
    this.props.data(this.getStyleData());
  });
}

Example

这篇关于动态反应设置状态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆